-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1678.Goal-Parser-Interpretation.java
45 lines (35 loc) · 1.24 KB
/
1678.Goal-Parser-Interpretation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// https://leetcode.com/problems/goal-parser-interpretation/
// algorithms
// Easy (91.37%)
// Total Accepted: 5,271
// Total Submissions: 5,769
// beats 100.0% of java submissions
class Solution {
private static final Map<String, String> mapping = new HashMap<>();
private static final String G = "G";
private static final String BRACKETS = "()";
private static final String BRACKETS_AL = "(al)";
static {
mapping.put(G, "G");
mapping.put(BRACKETS, "o");
mapping.put(BRACKETS_AL, "al");
}
public String interpret(String command) {
StringBuilder sb = new StringBuilder();
int len = command.length();
int idx = 0;
while (idx < len) {
if (G.equals(command.substring(idx, idx + 1))) {
sb.append(mapping.get(G));
idx++;
} else if (BRACKETS.equals(command.substring(idx, idx + BRACKETS.length()))) {
sb.append(mapping.get(BRACKETS));
idx += BRACKETS.length();
} else {
sb.append(mapping.get(BRACKETS_AL));
idx += BRACKETS_AL.length();
}
}
return sb.toString();
}
}