Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 71facdf

Browse files
committedMar 25, 2025
Add solution #921
1 parent 2263d6e commit 71facdf

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@
730730
918|[Maximum Sum Circular Subarray](./solutions/0918-maximum-sum-circular-subarray.js)|Medium|
731731
919|[Complete Binary Tree Inserter](./solutions/0919-complete-binary-tree-inserter.js)|Medium|
732732
920|[Number of Music Playlists](./solutions/0920-number-of-music-playlists.js)|Hard|
733+
921|[Minimum Add to Make Parentheses Valid](./solutions/0921-minimum-add-to-make-parentheses-valid.js)|Medium|
733734
922|[Sort Array By Parity II](./solutions/0922-sort-array-by-parity-ii.js)|Easy|
734735
925|[Long Pressed Name](./solutions/0925-long-pressed-name.js)|Easy|
735736
926|[Flip String to Monotone Increasing](./solutions/0926-flip-string-to-monotone-increasing.js)|Medium|
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 921. Minimum Add to Make Parentheses Valid
3+
* https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
4+
* Difficulty: Medium
5+
*
6+
* A parentheses string is valid if and only if:
7+
* - It is the empty string,
8+
* - It can be written as AB (A concatenated with B), where A and B are valid strings, or
9+
* - It can be written as (A), where A is a valid string.
10+
*
11+
* You are given a parentheses string s. In one move, you can insert a parenthesis at any position
12+
* of the string.
13+
* - For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing
14+
* parenthesis to be "())))".
15+
*
16+
* Return the minimum number of moves required to make s valid.
17+
*/
18+
19+
/**
20+
* @param {string} s
21+
* @return {number}
22+
*/
23+
var minAddToMakeValid = function(s) {
24+
let openCount = 0;
25+
let unmatchedClose = 0;
26+
27+
for (const character of s) {
28+
if (character === '(') {
29+
openCount++;
30+
} else if (openCount > 0) {
31+
openCount--;
32+
} else {
33+
unmatchedClose++;
34+
}
35+
}
36+
37+
return openCount + unmatchedClose;
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.