File tree Expand file tree Collapse file tree 4 files changed +170
-0
lines changed
solution/1800-1899/1807.Evaluate the Bracket Pairs of a String Expand file tree Collapse file tree 4 files changed +170
-0
lines changed Original file line number Diff line number Diff line change @@ -188,6 +188,66 @@ func evaluate(s string, knowledge [][]string) string {
188
188
}
189
189
```
190
190
191
+ ### ** TypeScript**
192
+
193
+ ``` ts
194
+ function evaluate(s : string , knowledge : string [][]): string {
195
+ const n = s .length ;
196
+ const map = new Map ();
197
+ for (const [k, v] of knowledge ) {
198
+ map .set (k , v );
199
+ }
200
+ const ans = [];
201
+ let i = 0 ;
202
+ while (i < n ) {
203
+ if (s [i ] === ' (' ) {
204
+ const j = s .indexOf (' )' , i + 1 );
205
+ ans .push (map .get (s .slice (i + 1 , j )) ?? ' ?' );
206
+ i = j ;
207
+ } else {
208
+ ans .push (s [i ]);
209
+ }
210
+ i ++ ;
211
+ }
212
+ return ans .join (' ' );
213
+ }
214
+ ```
215
+
216
+ ### ** Rust**
217
+
218
+ ``` rust
219
+ use std :: collections :: HashMap ;
220
+ impl Solution {
221
+ pub fn evaluate (s : String , knowledge : Vec <Vec <String >>) -> String {
222
+ let s = s . as_bytes ();
223
+ let n = s . len ();
224
+ let mut map = HashMap :: new ();
225
+ for v in knowledge . iter () {
226
+ map . insert (& v [0 ], & v [1 ]);
227
+ }
228
+ let mut ans = String :: new ();
229
+ let mut i = 0 ;
230
+ while i < n {
231
+ if s [i ] == b '(' {
232
+ i += 1 ;
233
+ let mut j = i ;
234
+ let mut key = String :: new ();
235
+ while s [j ] != b ')' {
236
+ key . push (s [j ] as char );
237
+ j += 1 ;
238
+ }
239
+ ans . push_str (map . get (& key ). unwrap_or (&& '?' . to_string ()));
240
+ i = j ;
241
+ } else {
242
+ ans . push (s [i ] as char );
243
+ }
244
+ i += 1 ;
245
+ }
246
+ ans
247
+ }
248
+ }
249
+ ```
250
+
191
251
### ** ...**
192
252
193
253
```
Original file line number Diff line number Diff line change @@ -170,6 +170,66 @@ func evaluate(s string, knowledge [][]string) string {
170
170
}
171
171
```
172
172
173
+ ### ** TypeScript**
174
+
175
+ ``` ts
176
+ function evaluate(s : string , knowledge : string [][]): string {
177
+ const n = s .length ;
178
+ const map = new Map ();
179
+ for (const [k, v] of knowledge ) {
180
+ map .set (k , v );
181
+ }
182
+ const ans = [];
183
+ let i = 0 ;
184
+ while (i < n ) {
185
+ if (s [i ] === ' (' ) {
186
+ const j = s .indexOf (' )' , i + 1 );
187
+ ans .push (map .get (s .slice (i + 1 , j )) ?? ' ?' );
188
+ i = j ;
189
+ } else {
190
+ ans .push (s [i ]);
191
+ }
192
+ i ++ ;
193
+ }
194
+ return ans .join (' ' );
195
+ }
196
+ ```
197
+
198
+ ### ** Rust**
199
+
200
+ ``` rust
201
+ use std :: collections :: HashMap ;
202
+ impl Solution {
203
+ pub fn evaluate (s : String , knowledge : Vec <Vec <String >>) -> String {
204
+ let s = s . as_bytes ();
205
+ let n = s . len ();
206
+ let mut map = HashMap :: new ();
207
+ for v in knowledge . iter () {
208
+ map . insert (& v [0 ], & v [1 ]);
209
+ }
210
+ let mut ans = String :: new ();
211
+ let mut i = 0 ;
212
+ while i < n {
213
+ if s [i ] == b '(' {
214
+ i += 1 ;
215
+ let mut j = i ;
216
+ let mut key = String :: new ();
217
+ while s [j ] != b ')' {
218
+ key . push (s [j ] as char );
219
+ j += 1 ;
220
+ }
221
+ ans . push_str (map . get (& key ). unwrap_or (&& '?' . to_string ()));
222
+ i = j ;
223
+ } else {
224
+ ans . push (s [i ] as char );
225
+ }
226
+ i += 1 ;
227
+ }
228
+ ans
229
+ }
230
+ }
231
+ ```
232
+
173
233
### ** ...**
174
234
175
235
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+ impl Solution {
3
+ pub fn evaluate ( s : String , knowledge : Vec < Vec < String > > ) -> String {
4
+ let s = s. as_bytes ( ) ;
5
+ let n = s. len ( ) ;
6
+ let mut map = HashMap :: new ( ) ;
7
+ for v in knowledge. iter ( ) {
8
+ map. insert ( & v[ 0 ] , & v[ 1 ] ) ;
9
+ }
10
+ let mut ans = String :: new ( ) ;
11
+ let mut i = 0 ;
12
+ while i < n {
13
+ if s[ i] == b'(' {
14
+ i += 1 ;
15
+ let mut j = i;
16
+ let mut key = String :: new ( ) ;
17
+ while s[ j] != b')' {
18
+ key. push ( s[ j] as char ) ;
19
+ j += 1 ;
20
+ }
21
+ ans. push_str ( map. get ( & key) . unwrap_or ( & & '?' . to_string ( ) ) ) ;
22
+ i = j;
23
+ } else {
24
+ ans. push ( s[ i] as char ) ;
25
+ }
26
+ i += 1 ;
27
+ }
28
+ ans
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ function evaluate ( s : string , knowledge : string [ ] [ ] ) : string {
2
+ const n = s . length ;
3
+ const map = new Map ( ) ;
4
+ for ( const [ k , v ] of knowledge ) {
5
+ map . set ( k , v ) ;
6
+ }
7
+ const ans = [ ] ;
8
+ let i = 0 ;
9
+ while ( i < n ) {
10
+ if ( s [ i ] === '(' ) {
11
+ const j = s . indexOf ( ')' , i + 1 ) ;
12
+ ans . push ( map . get ( s . slice ( i + 1 , j ) ) ?? '?' ) ;
13
+ i = j ;
14
+ } else {
15
+ ans . push ( s [ i ] ) ;
16
+ }
17
+ i ++ ;
18
+ }
19
+ return ans . join ( '' ) ;
20
+ }
You can’t perform that action at this time.
0 commit comments