Skip to content

1678. Goal Parser Interpretation 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  string interpret(string command) {
    string ans;
    for (int i = 0; i < command.size();)
      if (command[i] == 'G') {
        ans += "G";
        ++i;
      } else if (command[i + 1] == ')') {
        ans += "o";
        i += 2;
      } else {
        ans += "al";
        i += 4;
      }
    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
  public String interpret(String command) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < command.length();)
      if (command.charAt(i) == 'G') {
        sb.append("G");
        ++i;
      } else if (command.charAt(i + 1) == ')') {
        sb.append("o");
        i += 2;
      } else {
        sb.append("al");
        i += 4;
      }
    return sb.toString();
  }
}
1
2
3
class Solution:
  def interpret(self, command: str) -> str:
    return command.replace('()', 'o').replace('(al)', 'al')