Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 134f714

Browse files
authored
Create Evaluate the Bracket Pairs of a String.java
1 parent 0c299dc commit 134f714

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public String evaluate(String s, List<List<String>> knowledge) {
3+
Map<String, String> map = new HashMap<>();
4+
for (List<String> entry : knowledge) {
5+
map.put(entry.get(0), entry.get(1));
6+
}
7+
StringBuilder sb = new StringBuilder();
8+
int idx = 0;
9+
int n = s.length();
10+
while (idx < n) {
11+
if (s.charAt(idx) == '(') {
12+
idx++;
13+
int currIdx = idx;
14+
while (idx < n && s.charAt(idx) != ')') {
15+
idx++;
16+
}
17+
sb.append(map.getOrDefault(s.substring(currIdx, idx++), "?"));
18+
} else {
19+
sb.append(s.charAt(idx++));
20+
}
21+
}
22+
return sb.toString();
23+
}
24+
}

0 commit comments

Comments
 (0)