forked from ignacio-chiazzo/Algorithms-Leetcode-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum_Add_To_Make_Parentheses_Valid.js
69 lines (54 loc) · 1.61 KB
/
Minimum_Add_To_Make_Parentheses_Valid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Minimum Add to Make Parentheses Valid
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
A parentheses string is valid if and only if:
It is the empty string,
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
Return the minimum number of moves required to make s valid.
Example 1:
Input: s = "())"
Output: 1
Example 2:
Input: s = "((("
Output: 3
Constraints:
1 <= s.length <= 1000
s[i] is either '(' or ')'.
*/
var minAddToMakeValid = function(s) {
var opening = 0;
var extraParClosing = 0;
for(let i = 0; i < s.length; i++) {
if(s.charAt(i) == "(") {
opening++;
} else if(s.charAt(i) == ")") {
if(opening == 0) {
extraParClosing++;
} else {
opening--;;
}
}
}
return extraParClosing + opening;
};
// Solution 2 using a queue
var minAddToMakeValidUsingQueue = function(s) {
var queue = [];
var extraParClosing = 0;
for(let i = 0; i < s.length; i++) {
if(s.charAt(i) == "(") {
queue.push(s.charAt(i))
} else if(s.charAt(i) == ")") {
if(queue.length > 0) {
queue.pop();
} else {
extraParClosing++;
}
}
}
return extraParClosing + queue.length;
};
module.exports.minAddToMakeValid = minAddToMakeValid;