Skip to content

Latest commit

 

History

History
33 lines (31 loc) · 803 Bytes

1807.md

File metadata and controls

33 lines (31 loc) · 803 Bytes

1807. Evaluate the Bracket Pairs of a String

Solution 1 (time O(n), space O(n))

class Solution(object):
    def evaluate(self, s, knowledge):
        """
        :type s: str
        :type knowledge: List[List[str]]
        :rtype: str
        """
        d = {k: v for k, v in knowledge}
        n = len(s)
        ans = ""
        p = 0
        while p < n:
            if s[p] == "(":
                p += 1
                t = ""
                while s[p] != ")":
                    t += s[p]
                    p += 1
                p += 1
                if t in d:
                    ans += d[t]
                else:
                    ans += "?"
            else:
                ans += s[p]
                p += 1
        return ans