其他分享
首页 > 其他分享> > 【Leetcode】1678. Goal Parser Interpretation

【Leetcode】1678. Goal Parser Interpretation

作者:互联网

题目地址:

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

给定一个只含"G", "()", "(al)"的各种拼接组成的字符串 s s s,要求将"()"解析为"o",将"(al)"解析为"al",并按原顺序返回。

直接解析。代码如下:

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

时空复杂度 O ( l s ) O(l_s) O(ls​)。

标签:ch,Interpretation,Parser,al,1678,sb,command,解析,append
来源: https://blog.csdn.net/qq_46105170/article/details/110791854