File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments