设计Goal解析器
示例
输入:command = “G()(al)”
输出:“Goal”
解释:Goal 解析器解释命令的步骤如下所示:
G -> G
() -> o
(al) -> al
最后连接得到的结果是 “Goal”
输入:command = “G()()()()(al)”
输出:“Gooooal”
输入:command = “(al)G(al)()()G”
输出:“alGalooG”
// C++
class Solution {
public:
string interpret(string command) {
string res;
for (int i = 0; i < command.size(); i++) {
if (command[i] == 'G') {
res += "G";
} else if (command[i] == '(') {
if (command[i + 1] == ')') {
res += "o";
} else {
res += "al";
}
}
}
return res;
}
};