题目地址:
https://leetcode.com/problems/goal-parser-interpretation/
给定一个只含"G", "()", "(al)"
的各种拼接组成的字符串
s
s
s,要求将"()"
解析为"o"
,将"(al)"
解析为"al"
,并按原顺序返回。
直接解析。代码如下:
public class Solution {
public String interpret(String command) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < command.length(); i++) {
char ch = command.charAt(i);
if (ch == 'G') {
sb.append('G');
} else {
if (command.charAt(i + 1) == ')') {
sb.append('o');
i++;
} else {
sb.append("al");
i = i + 3;
}
}
}
return sb.toString();
}
}
时空复杂度 O ( l s ) O(l_s) O(ls)。