File tree Expand file tree Collapse file tree 4 files changed +76
-0
lines changed
solution/0900-0999/0921.Minimum Add to Make Parentheses Valid Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -286,4 +286,36 @@ function minAddToMakeValid(s: string): number {
286
286
287
287
<!-- solution: end -->
288
288
289
+ <!-- solution: start -->
290
+
291
+ ### Solution 3: Replace + recursion
292
+
293
+ <!-- tabs: start -->
294
+
295
+ #### TypeScript
296
+
297
+ ``` ts
298
+ function minAddToMakeValid(s : string ): number {
299
+ const l = s .length ;
300
+ s = s .replace (' ()' , ' ' );
301
+
302
+ return s .length === l ? l : minAddToMakeValid (s );
303
+ }
304
+ ```
305
+
306
+ #### JavaScript
307
+
308
+ ``` js
309
+ function minAddToMakeValid (s : string ): number {
310
+ const l = s .length ;
311
+ s = s .replace (' ()' , ' ' );
312
+
313
+ return s .length === l ? l : minAddToMakeValid (s);
314
+ }
315
+ ```
316
+
317
+ <!-- tabs: end -->
318
+
319
+ <!-- solution: end -->
320
+
289
321
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -284,4 +284,36 @@ function minAddToMakeValid(s: string): number {
284
284
285
285
<!-- solution: end -->
286
286
287
+ <!-- solution: start -->
288
+
289
+ ### Solution 3: Replace + recursion
290
+
291
+ <!-- tabs: start -->
292
+
293
+ #### TypeScript
294
+
295
+ ``` ts
296
+ function minAddToMakeValid(s : string ): number {
297
+ const l = s .length ;
298
+ s = s .replace (' ()' , ' ' );
299
+
300
+ return s .length === l ? l : minAddToMakeValid (s );
301
+ }
302
+ ```
303
+
304
+ #### JavaScript
305
+
306
+ ``` js
307
+ function minAddToMakeValid (s : string ): number {
308
+ const l = s .length ;
309
+ s = s .replace (' ()' , ' ' );
310
+
311
+ return s .length === l ? l : minAddToMakeValid (s);
312
+ }
313
+ ```
314
+
315
+ <!-- tabs: end -->
316
+
317
+ <!-- solution: end -->
318
+
287
319
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function minAddToMakeValid ( s : string ) : number {
2
+ const l = s . length ;
3
+ s = s . replace ( '()' , '' ) ;
4
+
5
+ return s . length === l ? l : minAddToMakeValid ( s ) ;
6
+ }
Original file line number Diff line number Diff line change
1
+ function minAddToMakeValid ( s : string ) : number {
2
+ const l = s . length ;
3
+ s = s . replace ( '()' , '' ) ;
4
+
5
+ return s . length === l ? l : minAddToMakeValid ( s ) ;
6
+ }
You can’t perform that action at this time.
0 commit comments