首页 > 其他分享 >[LeetCode] 1678. Goal Parser Interpretation

[LeetCode] 1678. Goal Parser Interpretation

时间:2022-11-07 07:22:05浏览次数:76  
标签:Interpretation Goal Parser al 1678 字符串 command string

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.

设计 Goal 解析器。

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

给你字符串 command ,返回 Goal 解析器 对 command 的解释结果。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/goal-parser-interpretation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道字符串操作题。一开始我想复杂了,题目中间涉及的字母一定是按顺序出现的,一定是先有了G,之后才会有 O 和 AL,只是因为 O 的数量不确定,所以需要判断一下到底有多少个。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public String interpret(String command) {
 3         StringBuilder sb = new StringBuilder();
 4         int n = command.length();
 5         int i = 0;
 6         while (i < n) {
 7             if (command.charAt(i) == 'G') {
 8                 sb.append('G');
 9                 i++;
10             } else if (i + 1 < n && command.charAt(i + 1) == ')') {
11                 sb.append('o');
12                 i += 2;
13             } else {
14                 sb.append("al");
15                 i += 4;
16             }
17         }
18         return sb.toString();
19     }
20 }

 

LeetCode 题目总结

标签:Interpretation,Goal,Parser,al,1678,字符串,command,string
From: https://www.cnblogs.com/cnoodle/p/16864795.html

相关文章

  • 1678. 设计 Goal 解析器
    请你设计一个可以解释字符串command的Goal解析器。command由"G"、"()"和/或"(al)"按某种顺序组成。Goal解析器会将"G"解释为字符串"G"、"()"解释为字符串"......
  • 1678. 设计 Goal 解析器
    1678.设计Goal解析器请你设计一个可以解释字符串command的Goal解析器。command由"G"、"()"和/或"(al)"按某种顺序组成。Goal解析器会将"G"解释为字符串"......
  • 1678. 设计 Goal 解析器
    1678.设计Goal解析器classSolution{publicStringinterpret(Stringcommand){char[]ch=command.toCharArray();intn=ch.length;......
  • python之configparser解析ini文件
    login.ini文件内容如下[data1]username=zhangpassword=123456address=sichuan[data2]username=lipassword=654321address=guangdong通过configparser解析importconfigparse......
  • Python configparser模块
    1、configparser模块介绍:一般做自动化测试的时候,会使用到这个模块,用来操作配置文件(ini文件)封装一些常量。比如数据库、邮件、用户名密码、项目常量等等2、ini文件是一种......
  • ParserWarning: Falling back to the 'python' engine because the 'c' engine does n
    Python3.9.10,Window64bit   警告:ParserWarning:Fallingbacktothe'python'enginebecausethe'c'enginedoesnotsupportregexseparators(separators......
  • ini配置文件与ConfigParser对象
    ini配置文件与ConfigParser对象目录结构​​ini配置文件与ConfigParser对象​​​​ini配置文件简介​​​​configparser模块中的ConfigParser类​​​​常用方法​​​​......
  • Python 之configparser模块
    一、示例'''添加add_section(section)向实例添加一个sectionset(section,option,value)如果给定的部分存在,将给定的选项设置为指定的值optionxform(option......
  • Python Parser的用法
    ------2022年10月12日11:56:29-------注意,在解析parse中,对于可选参数选取最长的名称,中划线转换为下划线--------PythonParser的用法文章目录[隐藏]目录一、介绍......
  • python parser.parse_args action=‘store_true‘ 和 ‘store_false’
    store_true就是存储的值为true(store_false就是存储的值为false),用sh命令触发值的设置:parser.add_argument('-p',action='store_true',default=false)#pythontes......