字符串题目:设计 Goal 解析器

题目

标题和出处

标题:设计 Goal 解析器

出处:1678. 设计 Goal 解析器

难度

2 级

题目描述

要求

请你设计一个可以解释字符串 command \texttt{command} commandGoal 解析器 command \texttt{command} command "G" \texttt{"G"} "G" "()" \texttt{"()"} "()" 和/或 "(al)" \texttt{"(al)"} "(al)" 按某种顺序组成。Goal 解析器会将 "G" \texttt{"G"} "G" 解释为字符串 "G" \texttt{"G"} "G" "()" \texttt{"()"} "()" 解释为字符串 "o" \texttt{"o"} "o" "(al)" \texttt{"(al)"} "(al)" 解释为字符串 "al" \texttt{"al"} "al"。然后,按原顺序将经解释得到的字符串连接成一个字符串。

给你字符串 command \texttt{command} command,返回 Goal 解析器 command \texttt{command} command 的解释结果。

示例

示例 1:

输入: command   =   "G()(al)" \texttt{command = "G()(al)"} command = "G()(al)"
输出: "Goal" \texttt{"Goal"} "Goal"
解释:Goal 解析器解释命令的步骤如下所示:
G   ->   G \texttt{G -> G} G -> G
()   ->   o \texttt{() -> o} () -> o
(al)   ->   al \texttt{(al) -> al} (al) -> al
最后连接得到的结果是 "Goal" \texttt{"Goal"} "Goal"

示例 2:

输入: command   =   "G()()()()(al)" \texttt{command = "G()()()()(al)"} command = "G()()()()(al)"
输出: "Gooooal" \texttt{"Gooooal"} "Gooooal"

示例 3:

输入: command   =   "(al)G(al)()()G" \texttt{command = "(al)G(al)()()G"} command = "(al)G(al)()()G"
输出: "alGalooG" \texttt{"alGalooG"} "alGalooG"

数据范围

  • 1 ≤ command.length ≤ 100 \texttt{1} \le \texttt{command.length} \le \texttt{100} 1command.length100
  • command \texttt{command} command "G" \texttt{"G"} "G" "()" \texttt{"()"} "()" 和/或 "(al)" \texttt{"(al)"} "(al)" 按某种顺序组成

解法

思路和算法

由于给定的字符串 command \textit{command} command “G" \text{``G"} “G" “()" \text{``()"} “()" 和/或 “(al)" \text{``(al)"} “(al)" 按某种顺序组成,因此一定可以正确地解析字符串 command \textit{command} command,不需要考虑不合法的情况。

从左到右遍历字符串 command \textit{command} command,使用 StringBuffer \texttt{StringBuffer} StringBuffer 类型的变量存储解析之后的结果。具体而言,根据当前字符和下一个字符决定当前需要解析的字符个数。

  • 如果当前字符是 ‘G’ \text{`G'} ‘G’,则当前需要解析 1 1 1 个字符,解析结果是 “G" \text{``G"} “G"

  • 如果当前字符是 ‘(’ \text{`('} ‘(’,则根据下一个字符解析:

    • 如果下一个字符是 ‘)’ \text{`)'} ‘)’,则当前需要解析 2 2 2 个字符,解析结果是 “o" \text{``o"} “o"

    • 如果下一个字符不是 ‘)’ \text{`)'} ‘)’,则当前需要解析 4 4 4 个字符,解析结果是 “al" \text{``al"} “al"

遍历过程中维护下标值。每次将下标处的字符作为当前字符,解析之后,将下标值更新,继续对剩下部分解析。当下标值超出字符串的下标范围时,解析结束。

代码

class Solution {
    public String interpret(String command) {
        int length = command.length();
        StringBuffer sb = new StringBuffer();
        int index = 0;
        while (index < length) {
            char c = command.charAt(index);
            if (c == 'G') {
                sb.append("G");
                index++;
            } else {
                if (command.charAt(index + 1) == ')') {
                    sb.append("o");
                    index += 2;
                } else {
                    sb.append("al");
                    index += 4;
                }
            }
        }
        return sb.toString();
    }
}

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是字符串 command \textit{command} command 的长度。需要遍历字符串一次。

  • 空间复杂度: O ( n ) O(n) O(n),其中 n n n 是字符串 command \textit{command} command 的长度。需要创建一个 StringBuffer \texttt{StringBuffer} StringBuffer 类型的变量存储解析后的结果,长度不会超过 n n n

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大的车尔尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值