Skip to content

Commit e60887e

Browse files
Merge pull request ignacio-chiazzo#29 from ignacio-chiazzo/prob
remove invalid par
2 parents fdd07f9 + 1235229 commit e60887e

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Remove Invalid Parentheses
3+
https://leetcode.com/problems/remove-invalid-parentheses/
4+
5+
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.
6+
7+
Note: The input string may contain letters other than the parentheses ( and ).
8+
9+
Example 1:
10+
11+
Input: "()())()"
12+
Output: ["()()()", "(())()"]
13+
Example 2:
14+
15+
Input: "(a)())()"
16+
Output: ["(a)()()", "(a())()"]
17+
Example 3:
18+
19+
Input: ")("
20+
Output: [""]
21+
*/
22+
23+
/**
24+
* @param {string} s
25+
* @return {string[]}
26+
*/
27+
var removeInvalidParentheses = function(s) {
28+
var queue = [];
29+
var visited = new Set();
30+
queue.push(s);
31+
var result = [];
32+
var found = false;
33+
34+
while(queue.length !== 0) {
35+
var str = queue.shift();
36+
if(isValid(str)) {
37+
result.push(str);
38+
found = true;
39+
} else if(!found){
40+
for(var i = 0; i < s.length; i++) {
41+
if(str[i] === "(" || str[i] === ")") {
42+
var subStr = str.slice(0, i) + str.slice(i + 1, s.length);
43+
if(!visited.has(subStr)) {
44+
queue.push(subStr);
45+
visited.add(subStr);
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
return result;
53+
};
54+
55+
var isValid = function(s) {
56+
var leftCount = 0;
57+
var iter = 0;
58+
while(iter < s.length) {
59+
if(s[iter] === "(")
60+
leftCount++;
61+
else if(s[iter] === ")") {
62+
leftCount--;
63+
if(leftCount < 0)
64+
return false;
65+
}
66+
iter++;
67+
}
68+
69+
return leftCount === 0;
70+
}
71+
72+
var main = function() {
73+
console.log(removeInvalidParentheses("))))(()"));
74+
console.log(removeInvalidParentheses("(()"));
75+
console.log(removeInvalidParentheses("(d))()"));
76+
console.log(removeInvalidParentheses("(())"))
77+
}
78+
79+
module.exports.main = main;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Solutions of algorithm problems using Javascript
77
| Name | Level | Link |
88
| - | - | - |
99
| [Edit Distance ](/LeetcodeProblems/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ |
10+
| [Remove Invalid Parentheses ](/LeetcodeProblems/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ |
1011
| [Longest Consecutive Sequence ](/LeetcodeProblems/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ |
1112
| [Minimum Window Substring ](/LeetcodeProblems/Minimum_Window_Substring.js) | Hard | https://leetcode.com/problems/minimum-window-substring/ |
1213
| [Regular Expression Matching ](/LeetcodeProblems/Regular_Expression_Matching.js) | Hard | https://leetcode.com/problems/regular-expression-matching/ |

0 commit comments

Comments
 (0)