Skip to content

remove invalid par #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions LeetcodeProblems/Remove_Invalid_Parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Remove Invalid Parentheses
https://leetcode.com/problems/remove-invalid-parentheses/

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses ( and ).

Example 1:

Input: "()())()"
Output: ["()()()", "(())()"]
Example 2:

Input: "(a)())()"
Output: ["(a)()()", "(a())()"]
Example 3:

Input: ")("
Output: [""]
*/

/**
* @param {string} s
* @return {string[]}
*/
var removeInvalidParentheses = function(s) {
var queue = [];
var visited = new Set();
queue.push(s);
var result = [];
var found = false;

while(queue.length !== 0) {
var str = queue.shift();
if(isValid(str)) {
result.push(str);
found = true;
} else if(!found){
for(var i = 0; i < s.length; i++) {
if(str[i] === "(" || str[i] === ")") {
var subStr = str.slice(0, i) + str.slice(i + 1, s.length);
if(!visited.has(subStr)) {
queue.push(subStr);
visited.add(subStr);
}
}
}
}
}

return result;
};

var isValid = function(s) {
var leftCount = 0;
var iter = 0;
while(iter < s.length) {
if(s[iter] === "(")
leftCount++;
else if(s[iter] === ")") {
leftCount--;
if(leftCount < 0)
return false;
}
iter++;
}

return leftCount === 0;
}

var main = function() {
console.log(removeInvalidParentheses("))))(()"));
console.log(removeInvalidParentheses("(()"));
console.log(removeInvalidParentheses("(d))()"));
console.log(removeInvalidParentheses("(())"))
}

module.exports.main = main;
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Solutions of algorithm problems using Javascript
| Name | Level | Link |
| - | - | - |
| [Edit Distance ](/LeetcodeProblems/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ |
| [Remove Invalid Parentheses ](/LeetcodeProblems/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ |
| [Longest Consecutive Sequence ](/LeetcodeProblems/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ |
| [Minimum Window Substring ](/LeetcodeProblems/Minimum_Window_Substring.js) | Hard | https://leetcode.com/problems/minimum-window-substring/ |
| [Regular Expression Matching ](/LeetcodeProblems/Regular_Expression_Matching.js) | Hard | https://leetcode.com/problems/regular-expression-matching/ |
Expand Down