Skip to content

Commit 52c4569

Browse files
committed
feat: add solutions to lc problem: No.1678
No.1678.Goal Parser Interpretation
1 parent 05e8a44 commit 52c4569

File tree

4 files changed

+116
-42
lines changed

4 files changed

+116
-42
lines changed

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,29 +172,60 @@ func interpret(command string) string {
172172
}
173173
```
174174

175+
### **TypeScript**
176+
177+
```ts
178+
function interpret(command: string): string {
179+
return command.replace(/\(\)/g, 'o').replace(/\(al\)/g, 'al');
180+
}
181+
```
182+
183+
```ts
184+
function interpret(command: string): string {
185+
const n = command.length;
186+
const ans: string[] = [];
187+
for (let i = 0; i < n; i++) {
188+
const c = command[i];
189+
if (c === 'G') {
190+
ans.push(c);
191+
} else if (c === '(') {
192+
ans.push(command[i + 1] === ')' ? 'o' : 'al');
193+
}
194+
}
195+
return ans.join('');
196+
}
197+
```
198+
175199
### **Rust**
176200

177201
```rust
178202
impl Solution {
179203
pub fn interpret(command: String) -> String {
180-
const ss: [&str; 3] = ["G", "o", "al"];
181-
let n = command.len();
204+
command.replace("()", "o").replace("(al)", "al")
205+
}
206+
}
207+
```
208+
209+
```rust
210+
impl Solution {
211+
pub fn interpret(command: String) -> String {
212+
let mut ans = String::new();
182213
let bs = command.as_bytes();
183-
let mut res = String::new();
184-
let mut i = 0;
185-
while i < n {
214+
for i in 0..bs.len() {
186215
if bs[i] == b'G' {
187-
res.push_str(ss[0]);
188-
i += 1;
189-
} else if bs[i + 1] == b')' {
190-
res.push_str(ss[1]);
191-
i += 2
192-
} else {
193-
res.push_str(ss[2]);
194-
i += 4
216+
ans.push_str("G");
217+
}
218+
if bs[i] == b'(' {
219+
ans.push_str({
220+
if bs[i + 1] == b')' {
221+
"o"
222+
} else {
223+
"al"
224+
}
225+
})
195226
}
196227
}
197-
res
228+
ans
198229
}
199230
}
200231
```

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

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,60 @@ func interpret(command string) string {
150150
}
151151
```
152152

153+
### **TypeScript**
154+
155+
```ts
156+
function interpret(command: string): string {
157+
return command.replace(/\(\)/g, 'o').replace(/\(al\)/g, 'al');
158+
}
159+
```
160+
161+
```ts
162+
function interpret(command: string): string {
163+
const n = command.length;
164+
const ans: string[] = [];
165+
for (let i = 0; i < n; i++) {
166+
const c = command[i];
167+
if (c === 'G') {
168+
ans.push(c);
169+
} else if (c === '(') {
170+
ans.push(command[i + 1] === ')' ? 'o' : 'al');
171+
}
172+
}
173+
return ans.join('');
174+
}
175+
```
176+
153177
### **Rust**
154178

155179
```rust
156180
impl Solution {
157181
pub fn interpret(command: String) -> String {
158-
const ss: [&str; 3] = ["G", "o", "al"];
159-
let n = command.len();
182+
command.replace("()", "o").replace("(al)", "al")
183+
}
184+
}
185+
```
186+
187+
```rust
188+
impl Solution {
189+
pub fn interpret(command: String) -> String {
190+
let mut ans = String::new();
160191
let bs = command.as_bytes();
161-
let mut res = String::new();
162-
let mut i = 0;
163-
while i < n {
192+
for i in 0..bs.len() {
164193
if bs[i] == b'G' {
165-
res.push_str(ss[0]);
166-
i += 1;
167-
} else if bs[i + 1] == b')' {
168-
res.push_str(ss[1]);
169-
i += 2
170-
} else {
171-
res.push_str(ss[2]);
172-
i += 4
194+
ans.push_str("G");
195+
}
196+
if bs[i] == b'(' {
197+
ans.push_str({
198+
if bs[i + 1] == b')' {
199+
"o"
200+
} else {
201+
"al"
202+
}
203+
})
173204
}
174205
}
175-
res
206+
ans
176207
}
177208
}
178209
```
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
impl Solution {
22
pub fn interpret(command: String) -> String {
3-
const ss: [&str; 3] = ["G", "o", "al"];
4-
let n = command.len();
3+
let mut ans = String::new();
54
let bs = command.as_bytes();
6-
let mut res = String::new();
7-
let mut i = 0;
8-
while i < n {
5+
for i in 0..bs.len() {
96
if bs[i] == b'G' {
10-
res.push_str(ss[0]);
11-
i += 1;
12-
} else if bs[i + 1] == b')' {
13-
res.push_str(ss[1]);
14-
i += 2
15-
} else {
16-
res.push_str(ss[2]);
17-
i += 4
7+
ans.push_str("G");
8+
}
9+
if bs[i] == b'(' {
10+
ans.push_str({
11+
if bs[i + 1] == b')' {
12+
"o"
13+
} else {
14+
"al"
15+
}
16+
})
1817
}
1918
}
20-
res
19+
ans
2120
}
2221
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function interpret(command: string): string {
2+
const n = command.length;
3+
const ans: string[] = [];
4+
for (let i = 0; i < n; i++) {
5+
const c = command[i];
6+
if (c === 'G') {
7+
ans.push(c);
8+
} else if (c === '(') {
9+
ans.push(command[i + 1] === ')' ? 'o' : 'al');
10+
}
11+
}
12+
return ans.join('');
13+
}

0 commit comments

Comments
 (0)