[LeetCode] Goal Parser Interpretation

1678. Goal Parser Interpretation

You own a Goal Parser that can interpret a string command. The command consists of an alphabet of “G”, “()” and/or “(al)” in some order. The Goal Parser will interpret “G” as the string “G”, “()” as the string “o”, and “(al)” as the string “al”. The interpreted strings are then concatenated in the original order.

Given the string command, return the Goal Parser’s interpretation of command.

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
class Solution {
public:
string interpret(string command) {
stringstream ss;
for(int i = 0; i < command.length();) {
switch(command[i]) {
case 'G': {
ss<<command[i];
i++;
break;
}
case '(': {
if(command[i+1] == ')') {
ss<<'o';
i+=2;
} else {
ss<<"al";
i+=4;
}
break;
}
}
}
return ss.str();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/04/PS/LeetCode/goal-parser-interpretation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.