Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
| [1656. 设计有序流](https://leetcode.cn/problems/design-an-ordered-stream/) | [LeetCode 题解链接](https://leetcode.cn/problems/design-an-ordered-stream/solution/by-ac_oier-5pe8/) | 简单 | 🤩🤩🤩🤩 |
| [1662. 检查两个字符串数组是否相等](https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/solution/by-ac_oier-h0l6/) | 简单 | 🤩🤩🤩🤩 |
| [1672. 最富有客户的资产总量](https://leetcode-cn.com/problems/richest-customer-wealth/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/richest-customer-wealth/solution/by-ac_oier-ai19/) | 简单 | 🤩🤩🤩🤩 |
| [1678. 设计 Goal 解析器](https://leetcode.cn/problems/goal-parser-interpretation/) | [LeetCode 题解链接](https://leetcode.cn/problems/goal-parser-interpretation/solution/by-ac_oier-a00y/) | 简单 | 🤩🤩🤩🤩 |
| [1688. 比赛中的配对次数](https://leetcode-cn.com/problems/count-of-matches-in-tournament/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-of-matches-in-tournament/solution/gong-shui-san-xie-jian-dan-nao-jin-ji-zh-cx7a/) | 简单 | 🤩🤩🤩🤩🤩 |
| [1694. 重新格式化电话号码](https://leetcode.cn/problems/reformat-phone-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/reformat-phone-number/solution/by-ac_oier-82xq/) | 简单 | 🤩🤩🤩🤩 |
| [1700. 无法吃午餐的学生数量](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/) | [LeetCode 题解链接](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/solution/by-ac_oier-rvc3/) | 简单 | 🤩🤩🤩🤩🤩 |
Expand Down
117 changes: 117 additions & 0 deletions LeetCode/1671-1680/1678. 设计 Goal 解析器(简单).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
### 题目描述

这是 LeetCode 上的 **[1678. 设计 Goal 解析器](https://leetcode.cn/problems/goal-parser-interpretation/solution/by-ac_oier-a00y/)** ,难度为 **简单**。

Tag : 「模拟」



请你设计一个可以解释字符串 `command` 的 `Goal` 解析器 。

`command` 由 `"G"`、`"()"` 或 `"(al)"` 按某种顺序组成。`Goal` 解析器会将 `"G"` 解释为字符串 `"G"`、`"()"` 解释为字符串 `"o"` ,`"(al)"` 解释为字符串 `"al"` 。

然后,按原顺序将经解释得到的字符串连接成一个字符串。

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

示例 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 代码:
```Java
class Solution {
public String interpret(String s) {
StringBuilder sb = new StringBuilder();
int n = s.length();
for (int i = 0; i < n; ) {
if (s.charAt(i) == 'G') {
sb.append('G'); i++;
} else if (i + 1 < n && s.charAt(i + 1) == ')') {
sb.append('o'); i += 2;
} else {
sb.append("al"); i += 4;
}
}
return sb.toString();
}
}
```
TypeScript 代码:
```TypeScript
function interpret(s: string): string {
const n = s.length
let ans = ''
for (let i = 0; i < n; ) {
if (s[i] == 'G') {
ans += 'G'; i++
} else if (i + 1 < n && s[i + 1] == ')') {
ans += 'o'; i += 2
} else {
ans += 'al'; i += 4
}
}
return ans
}
```
Python 代码:
```Python
class Solution:
def interpret(self, s: str) -> str:
ans = ''
n, i = len(s), 0
while i < n:
if s[i] == 'G':
ans, i = ans + s[i], i + 1
elif i + 1 < n and s[i + 1] == ')':
ans, i = ans + 'o', i + 2
else:
ans, i = ans + 'al', i + 4
return ans
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(n)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.1678` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。