Skip to content

Commit d186a68

Browse files
committed
feat: add solutions to lc problem: No.1678. Goal Parser Interpretation
1 parent 4f8bc26 commit d186a68

File tree

5 files changed

+195
-2
lines changed

5 files changed

+195
-2
lines changed

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

+70-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ G -> G
4444
<li><code>command</code> 由 <code>"G"</code>、<code>"()"</code> 和/或 <code>"(al)"</code> 按某种顺序组成</li>
4545
</ul>
4646

47-
4847
## 解法
4948

5049
<!-- 这里可写通用的实现逻辑 -->
@@ -56,7 +55,28 @@ G -&gt; G
5655
<!-- 这里可写当前语言的特殊实现逻辑 -->
5756

5857
```python
58+
class Solution:
59+
def interpret(self, command: str) -> str:
60+
return command.replace('()', 'o').replace('(al)', 'al')
61+
```
5962

63+
```python
64+
class Solution:
65+
def interpret(self, command: str) -> str:
66+
res = ''
67+
i, n = 0, len(command)
68+
while i < n:
69+
c = command[i]
70+
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
6080
```
6181

6282
### **Java**
@@ -89,6 +109,55 @@ class Solution {
89109
}
90110
```
91111

112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
string interpret(string command) {
118+
string res = "";
119+
int i = 0, n = command.size();
120+
while (i < n) {
121+
char c = command[i];
122+
if (c == 'G') {
123+
res += "G";
124+
i += 1;
125+
} else if (c == '(' && command[i + 1] != ')') {
126+
res += "al";
127+
i += 4;
128+
} else {
129+
res += "o";
130+
i += 2;
131+
}
132+
}
133+
return res;
134+
}
135+
};
136+
```
137+
138+
### **Go**
139+
140+
```go
141+
func interpret(command string) string {
142+
var res string
143+
i, n := 0, len(command)
144+
for i < n {
145+
c := command[i]
146+
if c == 'G' {
147+
res += "G"
148+
i += 1
149+
} else if c == '(' && command[i+1] != ')' {
150+
res += "al"
151+
i += 4
152+
} else {
153+
res += "o"
154+
i += 2
155+
}
156+
}
157+
return res
158+
}
159+
```
160+
92161
### **...**
93162

94163
```

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

+70-1
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,35 @@ The final concatenated result is &quot;Goal&quot;.
4343
<li><code>command</code> consists of <code>&quot;G&quot;</code>, <code>&quot;()&quot;</code>, and/or <code>&quot;(al)&quot;</code> in some order.</li>
4444
</ul>
4545

46-
4746
## Solutions
4847

4948
<!-- tabs:start -->
5049

5150
### **Python3**
5251

5352
```python
53+
class Solution:
54+
def interpret(self, command: str) -> str:
55+
return command.replace('()', 'o').replace('(al)', 'al')
56+
```
5457

58+
```python
59+
class Solution:
60+
def interpret(self, command: str) -> str:
61+
res = ''
62+
i, n = 0, len(command)
63+
while i < n:
64+
c = command[i]
65+
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
5575
```
5676

5777
### **Java**
@@ -82,6 +102,55 @@ class Solution {
82102
}
83103
```
84104

105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
string interpret(string command) {
111+
string res = "";
112+
int i = 0, n = command.size();
113+
while (i < n) {
114+
char c = command[i];
115+
if (c == 'G') {
116+
res += "G";
117+
i += 1;
118+
} else if (c == '(' && command[i + 1] != ')') {
119+
res += "al";
120+
i += 4;
121+
} else {
122+
res += "o";
123+
i += 2;
124+
}
125+
}
126+
return res;
127+
}
128+
};
129+
```
130+
131+
### **Go**
132+
133+
```go
134+
func interpret(command string) string {
135+
var res string
136+
i, n := 0, len(command)
137+
for i < n {
138+
c := command[i]
139+
if c == 'G' {
140+
res += "G"
141+
i += 1
142+
} else if c == '(' && command[i+1] != ')' {
143+
res += "al"
144+
i += 4
145+
} else {
146+
res += "o"
147+
i += 2
148+
}
149+
}
150+
return res
151+
}
152+
```
153+
85154
### **...**
86155

87156
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string interpret(string command) {
4+
string res = "";
5+
int i = 0, n = command.size();
6+
while (i < n) {
7+
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+
}
18+
}
19+
return res;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func interpret(command string) string {
2+
var res string
3+
i, n := 0, len(command)
4+
for i < n {
5+
c := command[i]
6+
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
15+
}
16+
}
17+
return res
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def interpret(self, command: str) -> str:
3+
res = ''
4+
i, n = 0, len(command)
5+
while i < n:
6+
c = command[i]
7+
if c == 'G':
8+
res += c
9+
i += 1
10+
elif c == '(' and command[i + 1] != ')':
11+
res += 'al'
12+
i += 4
13+
else:
14+
res += 'o'
15+
i += 2
16+
return res

0 commit comments

Comments
 (0)