File tree Expand file tree Collapse file tree 4 files changed +116
-42
lines changed
solution/1600-1699/1678.Goal Parser Interpretation Expand file tree Collapse file tree 4 files changed +116
-42
lines changed Original file line number Diff line number Diff line change @@ -172,29 +172,60 @@ func interpret(command string) string {
172
172
}
173
173
```
174
174
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
+
175
199
### ** Rust**
176
200
177
201
``` rust
178
202
impl Solution {
179
203
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 ();
182
213
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 () {
186
215
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
+ })
195
226
}
196
227
}
197
- res
228
+ ans
198
229
}
199
230
}
200
231
```
Original file line number Diff line number Diff line change @@ -150,29 +150,60 @@ func interpret(command string) string {
150
150
}
151
151
```
152
152
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
+
153
177
### ** Rust**
154
178
155
179
``` rust
156
180
impl Solution {
157
181
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 ();
160
191
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 () {
164
193
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
+ })
173
204
}
174
205
}
175
- res
206
+ ans
176
207
}
177
208
}
178
209
```
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
2
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 ( ) ;
5
4
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 ( ) {
9
6
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
+ } )
18
17
}
19
18
}
20
- res
19
+ ans
21
20
}
22
21
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments