Skip to content

Commit 4e70ae5

Browse files
committed
feat: update solutions to lc problem: No.1678
No.1678.Goal Parser Interpretation
1 parent 8754817 commit 4e70ae5

File tree

8 files changed

+161
-169
lines changed

8 files changed

+161
-169
lines changed

solution/1600-1699/1678.Goal Parser Interpretation/README.md

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ G -> G
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:字符串替换**
52+
53+
根据题意,只需要将字符串 `command` 中的 `"()"` 替换为 `'o'``"(al)"` 替换为 `"al"` 即可。
54+
55+
**方法二:字符串遍历**
56+
57+
我们也可以遍历字符串 `command`,对于每个字符 $c$:
58+
59+
- 如果是 `'G'`,直接将 $c$ 添加到结果串中;
60+
- 如果是 `'('`,判断下一个字符是否是 `')'`,若是,将 `'o'` 添加到结果串中,否则,将 `"al"` 添加到结果串中。
61+
62+
遍历结束,返回结果串即可。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
65+
5166
<!-- tabs:start -->
5267

5368
### **Python3**
@@ -63,20 +78,13 @@ class Solution:
6378
```python
6479
class Solution:
6580
def interpret(self, command: str) -> str:
66-
res = ''
67-
i, n = 0, len(command)
68-
while i < n:
69-
c = command[i]
81+
ans = []
82+
for i, c in enumerate(command):
7083
if c == 'G':
71-
res += c
72-
i += 1
73-
elif c == '(' and command[i + 1] != ')':
74-
res += 'al'
75-
i += 4
76-
else:
77-
res += 'o'
78-
i += 2
79-
return res
84+
ans.append(c)
85+
elif c == '(':
86+
ans.append('o' if command[i + 1] == ')' else 'al')
87+
return ''.join(ans)
8088
```
8189

8290
### **Java**
@@ -86,24 +94,24 @@ class Solution:
8694
```java
8795
class Solution {
8896
public String interpret(String command) {
89-
StringBuilder sb = new StringBuilder();
90-
int p = 0, q = 1;
91-
for (; p < command.length(); p++, q++) {
92-
char c = command.charAt(p);
93-
if (c == 'G') sb.append('G');
94-
if (c == '(') {
95-
if (command.charAt(q) == ')') {
96-
sb.append("o");
97-
p++;
98-
q++;
99-
} else {
100-
sb.append("al");
101-
p += 2;
102-
q += 2;
103-
}
97+
return command.replace("()", "o").replace("(al)", "al");
98+
}
99+
}
100+
```
101+
102+
```java
103+
class Solution {
104+
public String interpret(String command) {
105+
StringBuilder ans = new StringBuilder();
106+
for (int i = 0; i < command.length(); ++i) {
107+
char c = command.charAt(i);
108+
if (c == 'G') {
109+
ans.append(c);
110+
} else if (c == '(') {
111+
ans.append(command.charAt(i + 1) == ')' ? "o" : "al");
104112
}
105113
}
106-
return sb.toString();
114+
return ans.toString();
107115
}
108116
}
109117
```
@@ -114,22 +122,24 @@ class Solution {
114122
class Solution {
115123
public:
116124
string interpret(string command) {
117-
string res = "";
118-
int i = 0, n = command.size();
119-
while (i < n) {
125+
while (command.find("()") != -1) command.replace(command.find("()"), 2, "o");
126+
while (command.find("(al)") != -1) command.replace(command.find("(al)"), 4, "al");
127+
return command;
128+
}
129+
};
130+
```
131+
132+
```cpp
133+
class Solution {
134+
public:
135+
string interpret(string command) {
136+
string ans;
137+
for (int i = 0; i < command.size(); ++i) {
120138
char c = command[i];
121-
if (c == 'G') {
122-
res += "G";
123-
i += 1;
124-
} else if (c == '(' && command[i + 1] != ')') {
125-
res += "al";
126-
i += 4;
127-
} else {
128-
res += "o";
129-
i += 2;
130-
}
139+
if (c == 'G') ans += c;
140+
else if (c == '(') ans += command[i + 1] == ')' ? "o" : "al";
131141
}
132-
return res;
142+
return ans;
133143
}
134144
};
135145
```
@@ -138,22 +148,27 @@ public:
138148

139149
```go
140150
func interpret(command string) string {
141-
var res string
142-
i, n := 0, len(command)
143-
for i < n {
144-
c := command[i]
151+
command = strings.ReplaceAll(command, "()", "o")
152+
command = strings.ReplaceAll(command, "(al)", "al")
153+
return command
154+
}
155+
```
156+
157+
```go
158+
func interpret(command string) string {
159+
ans := &strings.Builder{}
160+
for i, c := range command {
145161
if c == 'G' {
146-
res += "G"
147-
i += 1
148-
} else if c == '(' && command[i+1] != ')' {
149-
res += "al"
150-
i += 4
151-
} else {
152-
res += "o"
153-
i += 2
162+
ans.WriteRune(c)
163+
} else if c == '(' {
164+
if command[i+1] == ')' {
165+
ans.WriteByte('o')
166+
} else {
167+
ans.WriteString("al")
168+
}
154169
}
155170
}
156-
return res
171+
return ans.String()
157172
}
158173
```
159174

solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,45 +58,38 @@ class Solution:
5858
```python
5959
class Solution:
6060
def interpret(self, command: str) -> str:
61-
res = ''
62-
i, n = 0, len(command)
63-
while i < n:
64-
c = command[i]
61+
ans = []
62+
for i, c in enumerate(command):
6563
if c == 'G':
66-
res += c
67-
i += 1
68-
elif c == '(' and command[i + 1] != ')':
69-
res += 'al'
70-
i += 4
71-
else:
72-
res += 'o'
73-
i += 2
74-
return res
64+
ans.append(c)
65+
elif c == '(':
66+
ans.append('o' if command[i + 1] == ')' else 'al')
67+
return ''.join(ans)
7568
```
7669

7770
### **Java**
7871

7972
```java
8073
class Solution {
8174
public String interpret(String command) {
82-
StringBuilder sb = new StringBuilder();
83-
int p = 0, q = 1;
84-
for (; p < command.length(); p++, q++) {
85-
char c = command.charAt(p);
86-
if (c == 'G') sb.append('G');
87-
if (c == '(') {
88-
if (command.charAt(q) == ')') {
89-
sb.append("o");
90-
p++;
91-
q++;
92-
} else {
93-
sb.append("al");
94-
p += 2;
95-
q += 2;
96-
}
75+
return command.replace("()", "o").replace("(al)", "al");
76+
}
77+
}
78+
```
79+
80+
```java
81+
class Solution {
82+
public String interpret(String command) {
83+
StringBuilder ans = new StringBuilder();
84+
for (int i = 0; i < command.length(); ++i) {
85+
char c = command.charAt(i);
86+
if (c == 'G') {
87+
ans.append(c);
88+
} else if (c == '(') {
89+
ans.append(command.charAt(i + 1) == ')' ? "o" : "al");
9790
}
9891
}
99-
return sb.toString();
92+
return ans.toString();
10093
}
10194
}
10295
```
@@ -107,22 +100,24 @@ class Solution {
107100
class Solution {
108101
public:
109102
string interpret(string command) {
110-
string res = "";
111-
int i = 0, n = command.size();
112-
while (i < n) {
103+
while (command.find("()") != -1) command.replace(command.find("()"), 2, "o");
104+
while (command.find("(al)") != -1) command.replace(command.find("(al)"), 4, "al");
105+
return command;
106+
}
107+
};
108+
```
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
string interpret(string command) {
114+
string ans;
115+
for (int i = 0; i < command.size(); ++i) {
113116
char c = command[i];
114-
if (c == 'G') {
115-
res += "G";
116-
i += 1;
117-
} else if (c == '(' && command[i + 1] != ')') {
118-
res += "al";
119-
i += 4;
120-
} else {
121-
res += "o";
122-
i += 2;
123-
}
117+
if (c == 'G') ans += c;
118+
else if (c == '(') ans += command[i + 1] == ')' ? "o" : "al";
124119
}
125-
return res;
120+
return ans;
126121
}
127122
};
128123
```
@@ -131,22 +126,27 @@ public:
131126

132127
```go
133128
func interpret(command string) string {
134-
var res string
135-
i, n := 0, len(command)
136-
for i < n {
137-
c := command[i]
129+
command = strings.ReplaceAll(command, "()", "o")
130+
command = strings.ReplaceAll(command, "(al)", "al")
131+
return command
132+
}
133+
```
134+
135+
```go
136+
func interpret(command string) string {
137+
ans := &strings.Builder{}
138+
for i, c := range command {
138139
if c == 'G' {
139-
res += "G"
140-
i += 1
141-
} else if c == '(' && command[i+1] != ')' {
142-
res += "al"
143-
i += 4
144-
} else {
145-
res += "o"
146-
i += 2
140+
ans.WriteRune(c)
141+
} else if c == '(' {
142+
if command[i+1] == ')' {
143+
ans.WriteByte('o')
144+
} else {
145+
ans.WriteString("al")
146+
}
147147
}
148148
}
149-
return res
149+
return ans.String()
150150
}
151151
```
152152

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
class Solution {
22
public:
33
string interpret(string command) {
4-
string res = "";
5-
int i = 0, n = command.size();
6-
while (i < n) {
4+
string ans;
5+
for (int i = 0; i < command.size(); ++i) {
76
char c = command[i];
8-
if (c == 'G') {
9-
res += "G";
10-
i += 1;
11-
} else if (c == '(' && command[i + 1] != ')') {
12-
res += "al";
13-
i += 4;
14-
} else {
15-
res += "o";
16-
i += 2;
17-
}
7+
if (c == 'G') ans += c;
8+
else if (c == '(') ans += command[i + 1] == ')' ? "o" : "al";
189
}
19-
return res;
10+
return ans;
2011
}
2112
};
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
func interpret(command string) string {
2-
var res string
3-
i, n := 0, len(command)
4-
for i < n {
5-
c := command[i]
2+
ans := &strings.Builder{}
3+
for i, c := range command {
64
if c == 'G' {
7-
res += "G"
8-
i += 1
9-
} else if c == '(' && command[i+1] != ')' {
10-
res += "al"
11-
i += 4
12-
} else {
13-
res += "o"
14-
i += 2
5+
ans.WriteRune(c)
6+
} else if c == '(' {
7+
if command[i+1] == ')' {
8+
ans.WriteByte('o')
9+
} else {
10+
ans.WriteString("al")
11+
}
1512
}
1613
}
17-
return res
14+
return ans.String()
1815
}

0 commit comments

Comments
 (0)