leetcode.com/problems/goal-parser-interpretation/

 

Goal Parser Interpretation - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

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.

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".

String에 replace함수를 이용해서 해결하였다.
String.replace는 전체 치환
replaceFirst는 처음만
두 번째 이후부터는 substring으로 쪼개서 해결해야할 것 같다.

class Solution {
    public String interpret(String command) {
    	return command.replace("()","o").replace("(al)", "al");
    }
}

+ Recent posts