1678. Goal Parser Interpretation

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
string interpret(string command) {
int l=-1, r = -1 , n =command.size();
string str;
for(int i=0;i<n;++i){
if(command[i] == 'G') str+='G';
else if(command[i] == '(') l =i;
else if(command[i] ==')'){
if(i-l==1) str+='o';
else str+="al";
}
}
return str;
}
};

analysis

  • time complexity O(n)
  • space complexity O(1)