设计 Goal 解析器
请你设计一个可以解释字符串 command 的 Goal 解析器 。command 由 “G”、“()” 和/或 “(al)” 按某种顺序组成。Goal 解析器会将 “G” 解释为字符串 “G”、“()” 解释为字符串 “o” ,“(al)” 解释为字符串 “al” 。然后,按原顺序将经解释得到的字符串连接成一个字符串。
方法:直接遍历
根据题意可以知道字符串 command\textit{command}command 一定由三种不同的字符串 组合而成,我们按照以上规则进行转换即可得到转换后的结果。
class Solution:
def interpret(self, command: str) -> str:
res = []
for i, c in enumerate(command):
if c == 'G':
res.append(c)
elif c == '(':
res.append('o' if command[i + 1] == ')' else 'al')
return ''.join(res)