Skip to content

Commit c1f1afd

Browse files
authored
Create evaluate-the-bracket-pairs-of-a-string.cpp
1 parent 8087255 commit c1f1afd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Time: O(n + m)
2+
// Space: O(n + m)
3+
4+
class Solution {
5+
public:
6+
string evaluate(string s, vector<vector<string>>& knowledge) {
7+
unordered_map<string, string> lookup;
8+
for (const auto& pair : knowledge) {
9+
lookup[pair[0]] = pair[1];
10+
}
11+
string result, curr;
12+
bool has_pair = false;;
13+
for (int i = 0; i < size(s); ++i) {
14+
if (s[i] == '(') {
15+
has_pair = true;
16+
} else if (s[i] == ')') {
17+
has_pair = false;
18+
if (lookup.count(curr)) {
19+
for (const auto& c : lookup[curr]) {
20+
result.push_back(c);
21+
}
22+
} else {
23+
result.push_back('?');
24+
}
25+
curr.clear();
26+
} else if (has_pair) {
27+
curr.push_back(s[i]);
28+
} else {
29+
result.push_back(s[i]);
30+
}
31+
}
32+
return result;
33+
}
34+
};

0 commit comments

Comments
 (0)