Skip to content

Commit 42849c9

Browse files
committed
feat: add solutions to lc problem: No.0921
1 parent 631791e commit 42849c9

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,36 @@ function minAddToMakeValid(s: string): number {
286286

287287
<!-- solution:end -->
288288

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+
289321
<!-- problem:end -->

solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,36 @@ function minAddToMakeValid(s: string): number {
284284

285285
<!-- solution:end -->
286286

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+
287319
<!-- problem:end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
}

0 commit comments

Comments
 (0)