1807. Evaluate the Bracket Pairs of a String

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1807

class Solution:
    def evaluate(self, s: str, knowledge: List[List[str]]) -> str:
        d = {a: b for a, b in knowledge}
        i, n = 0, len(s)
        res = []
        while i < n:
            if s[i] == '(':
                j = s.find(')', i + 1)
                res.append(d.get(s[i + 1 : j], '?'))
                i = j
            else:
                res.append(s[i])
            i += 1
        return ''.join(res)