目次
トピック
1678.ゴールパーサーの
設計文字列コマンドを解釈できるゴールパーサーを設計してください。コマンドは、「G」、「()」、「(al)」の順で構成されます。ゴールパーサーは、「G」を文字列「G」、「()」を文字列「o」、「(al)」を文字列「al」として解釈します。次に、解釈された文字列を元の順序で1つの文字列に連結します。
文字列コマンドを指定して、ゴールパーサーによるコマンドの解釈結果を返します。
例1:
输入:command = "G()(al)"
输出:"Goal"
解释:Goal 解析器解释命令的步骤如下所示:
G -> G
() -> o
(al) -> al
最后连接得到的结果是 "Goal"
例2:
输入:command = "G()()()()(al)"
输出:"Gooooal"
例3:
输入:command = "(al)G(al)()()G"
输出:"alGalooG"
促す:
1 <= command.length <= 100
command 由 "G"、"()" 和/或 "(al)" 按某种顺序组成
問題解決(Java)
class Solution
{
public String interpret(String command)
{
String result = "";
for(int index = 0;index < command.length();index++)
{
//如果为字母G
if(command.charAt(index) == 'G')
{
result+="G";
}
//如果为()
else if(command.charAt(index) == '(' && command.charAt(index + 1) == ')')
{
result+="o";
index++;
}
//如果为(al)
else if(command.charAt(index) == '(' && command.charAt(index + 1) == 'a')
{
result+="al";
index+=3;
}
}
return result;
}
}