Skip to content

Latest commit

 

History

History
186 lines (157 loc) · 4.53 KB

File metadata and controls

186 lines (157 loc) · 4.53 KB

中文文档

Description

You own a Goal Parser that can interpret a string command. The command consists of an alphabet of "G", "()" and/or "(al)" in some order. The Goal Parser will interpret "G" as the string "G", "()" as the string "o", and "(al)" as the string "al". The interpreted strings are then concatenated in the original order.

Given the string command, return the Goal Parser's interpretation of command.

 

Example 1:

Input: command = "G()(al)"
Output: "Goal"
Explanation: The Goal Parser interprets the command as follows:
G -> G
() -> o
(al) -> al
The final concatenated result is "Goal".

Example 2:

Input: command = "G()()()()(al)"
Output: "Gooooal"

Example 3:

Input: command = "(al)G(al)()()G"
Output: "alGalooG"

 

Constraints:

  • 1 <= command.length <= 100
  • command consists of "G", "()", and/or "(al)" in some order.

Solutions

Python3

class Solution:
    def interpret(self, command: str) -> str:
        return command.replace('()', 'o').replace('(al)', 'al')
class Solution:
    def interpret(self, command: str) -> str:
        res = ''
        i, n = 0, len(command)
        while i < n:
            c = command[i]
            if c == 'G':
                res += c
                i += 1
            elif c == '(' and command[i + 1] != ')':
                res += 'al'
                i += 4
            else:
                res += 'o'
                i += 2
        return res

Java

class Solution {
    public String interpret(String command) {
        StringBuilder sb = new StringBuilder();
        int p = 0, q = 1;
        for (; p < command.length(); p++, q++) {
            char c = command.charAt(p);
            if (c == 'G') sb.append('G');
            if (c == '(') {
                if (command.charAt(q) == ')') {
                    sb.append("o");
                    p++;
                    q++;
                } else {
                    sb.append("al");
                    p += 2;
                    q += 2;
                }
            }
        }
        return sb.toString();
    }
}

C++

class Solution {
public:
    string interpret(string command) {
        string res = "";
        int i = 0, n = command.size();
        while (i < n) {
            char c = command[i];
            if (c == 'G') {
                res += "G";
                i += 1;
            } else if (c == '(' && command[i + 1] != ')') {
                res += "al";
                i += 4;
            } else {
                res += "o";
                i += 2;
            }
        }
        return res;
    }
};

Go

func interpret(command string) string {
	var res string
	i, n := 0, len(command)
	for i < n {
		c := command[i]
		if c == 'G' {
			res += "G"
			i += 1
		} else if c == '(' && command[i+1] != ')' {
			res += "al"
			i += 4
		} else {
			res += "o"
			i += 2
		}
	}
	return res
}

Rust

impl Solution {
    pub fn interpret(command: String) -> String {
        const ss: [&str; 3] = ["G", "o", "al"];
        let n = command.len();
        let bs = command.as_bytes();
        let mut res = String::new();
        let mut i = 0;
        while i < n {
            if bs[i] == b'G' {
                res.push_str(ss[0]);
                i += 1;
            } else if bs[i + 1] == b')' {
                res.push_str(ss[1]);
                i += 2
            } else {
                res.push_str(ss[2]);
                i += 4
            }
        }
        res
    }
}

...