Skip to content

Commit 0cea665

Browse files
authored
Create evaluate-the-bracket-pairs-of-a-string.py
1 parent c1f1afd commit 0cea665

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n + m)
2+
# Space: O(n + m)
3+
4+
class Solution(object):
5+
def evaluate(self, s, knowledge):
6+
"""
7+
:type s: str
8+
:type knowledge: List[List[str]]
9+
:rtype: str
10+
"""
11+
lookup = {k: v for k, v in knowledge}
12+
result, curr = [], []
13+
has_pair = False
14+
for c in s:
15+
if c == '(':
16+
has_pair = True
17+
elif c == ')':
18+
has_pair = False
19+
result.append(lookup.get("".join(curr), '?'))
20+
curr = []
21+
elif is_pair:
22+
curr.append(c)
23+
else:
24+
result.append(c)
25+
return "".join(result)

0 commit comments

Comments
 (0)