-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoalParserInterpreter.cpp
More file actions
28 lines (27 loc) · 984 Bytes
/
GoalParserInterpreter.cpp
File metadata and controls
28 lines (27 loc) · 984 Bytes
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
// https://leetcode.com/problems/goal-parser-interpretation/submissions/
// Time complexity: O(n) where n is the length of the input string because
// we run through each element of the input string once
// Space complexity: O(n) where n is the length of the input string because
// the output string could have an upper bound of the input string length i.e. one thousand "G"s
string interpret(string command) {
//vector<string> chars = ["G", "()", "(al)"];
string temp = "";
string output = "";
for(int i = 0; i < command.length(); i++)
{
temp += command[i];
if(temp == "G"){
output.append("G");
temp = "";
}
else if(temp == "()"){
output.append("o");
temp = "";
}
else if(temp == "(al)"){
output.append("al");
temp = "";
}
}
return output;
}