Skip to content

Commit 5ab2faf

Browse files
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
1 parent 08a8c3b commit 5ab2faf

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Minimum Add to Make Parentheses Valid
3+
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
4+
5+
A parentheses string is valid if and only if:
6+
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+
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
11+
12+
For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
13+
Return the minimum number of moves required to make s valid.
14+
15+
Example 1:
16+
17+
Input: s = "())"
18+
Output: 1
19+
Example 2:
20+
21+
Input: s = "((("
22+
Output: 3
23+
24+
Constraints:
25+
26+
1 <= s.length <= 1000
27+
s[i] is either '(' or ')'.
28+
*/
29+
30+
31+
var minAddToMakeValid = function(s) {
32+
var opening = 0;
33+
var extraParClosing = 0;
34+
35+
for(let i = 0; i < s.length; i++) {
36+
if(s.charAt(i) == "(") {
37+
opening++;
38+
} else if(s.charAt(i) == ")") {
39+
if(opening == 0) {
40+
extraParClosing++;
41+
} else {
42+
opening--;;
43+
}
44+
}
45+
}
46+
return extraParClosing + opening;
47+
};
48+
49+
// Solution 2 using a queue
50+
var minAddToMakeValidUsingQueue = function(s) {
51+
var queue = [];
52+
var extraParClosing = 0;
53+
54+
for(let i = 0; i < s.length; i++) {
55+
if(s.charAt(i) == "(") {
56+
queue.push(s.charAt(i))
57+
} else if(s.charAt(i) == ")") {
58+
if(queue.length > 0) {
59+
queue.pop();
60+
} else {
61+
extraParClosing++;
62+
}
63+
}
64+
}
65+
66+
return extraParClosing + queue.length;
67+
};
68+
69+
module.exports.minAddToMakeValid = minAddToMakeValid;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const assert = require('assert');
2+
const minAddToMakeValid = require('../../LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid').minAddToMakeValid;
3+
4+
var test = function() {
5+
assert.strictEqual(1, minAddToMakeValid("()("));
6+
assert.strictEqual(3, minAddToMakeValid("((("));
7+
assert.strictEqual(0, minAddToMakeValid("()"));
8+
assert.strictEqual(1, minAddToMakeValid(")"));
9+
assert.strictEqual(1, minAddToMakeValid("("));
10+
assert.strictEqual(2, minAddToMakeValid(")("));
11+
};
12+
13+
module.exports.test = test;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
4343
| [Max Area Of Island ](/LeetcodeProblems/Algorithms/Max_Area_Of_Island.js) | Medium | https://leetcode.com/problems/max-area-of-island/ |
4444
| [Max Consecutive Ones III ](/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js) | Medium | https://leetcode.com/problems/max-consecutive-ones-iii |
4545
| [Maximal Square ](/LeetcodeProblems/Algorithms/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ |
46+
| [Minimum Add to Make Parentheses Valid ](/LeetcodeProblems/Algorithms/Minimum_Add_To_Make_Parentheses_Valid.js) | Medium | https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/ |
4647
| [Minimum Size Subarray](/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js) | Medium | https://leetcode.com/problems/minimum-size-subarray-sum |
4748
| [Permutations ](/LeetcodeProblems/Algorithms/Permutations.js) | Medium | https://leetcode.com/problems/permutations/ |
4849
| [Permutations II ](/LeetcodeProblems/Algorithms/Permutations_II.js) | Medium | https://leetcode.com/problems/permutations-ii/ |

0 commit comments

Comments
 (0)