Skip to content

Commit 924cf8b

Browse files
committed
Problem 171: Remove invalid parenthesis
1 parent d27f1d9 commit 924cf8b

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ build/
3535
#vim backup
3636
*.cpp~
3737
*~
38+
39+
#VS code
40+
.vscode

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 170 |
9+
| Total Problems | 171 |
1010

1111
</center>
1212

@@ -242,4 +242,5 @@ Include contains single header implementation of data structures and some algori
242242
|Given a sorted array, remove duplicates in place and return the new length. It doesn't matter what is in array beyond the unique elements size. Expected O(1) space and O(n) time complexity.| [remove_duplicates.cpp](leet_code_problems/remove_duplicates.cpp) |
243243
| Count the number of islands in a grid. Given a grid representing 1 as land body, and 0 as water body, determine the number of islands (more details in problem comments)|[count_islands.cpp](leet_code_problems/count_islands.cpp)|
244244
| Find median from a data stream. Design a data structure that supports addNum to add a number to the stream, and findMedian to return the median of the current numbers seen so far. Also, if the count of numbers is even, return average of two middle elements, return median otherwise.|[median_stream.cpp](leet_code_problems/median_stream.cpp)
245+
| 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 ) | [remove_invalid_parenthesis.cpp](leet_code_problems/remove_invalid_parenthesis.cpp)|
245246

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Remove the minimum number of invalid parentheses in order to make the input string valid.
3+
* Return all possible results.
4+
* Note: The input string may contain letters other than the parentheses ( and ).
5+
*
6+
* Source: Leetcode, 301. Remove Invalid Parentheses
7+
*
8+
* solution approach:
9+
*
10+
* We can solve this problem in many ways, I am choosing DFS.
11+
* For a string to be valid, we have two requirements:
12+
* 1. The number of left and right parenthesis should be same.
13+
* 2. They should be in right order, for each open there has to be a close, so for example '()' is valid
14+
* but ')(' is not.
15+
*
16+
* 1. We track the count of left and right parenthesis which are out of place.
17+
* 2. We recursively backtrack and go two ways.
18+
* If a character is left or right parenthesis,
19+
* a. we keep it
20+
* b. or we drop it.
21+
* 3. In both the cases we adjust open left and right parameters accordingly.
22+
* Open is the count of parenthesis sets which are open but not close yet.
23+
* When we reach end of string, we check if it is valid by left, right and open count and
24+
* push the string to result set.
25+
* 4. If a character is not a parenthesis operator, we simply add it to the result string.
26+
* 5. At the end of backtracking, we simply replace the current result string with the one
27+
* we started this iteration with.
28+
*
29+
* We are using a set to keep the results unique.
30+
*
31+
*/
32+
33+
34+
#include <iostream>
35+
#include <string>
36+
#include <unordered_set>
37+
#include <vector>
38+
39+
void dfs(const std::string& s, std::string temp, int index, int left, int right, int open,
40+
std::unordered_set<std::string>& result)
41+
{
42+
if (index == s.length()) {
43+
if (left == 0 && right == 0 && open == 0) {
44+
result.insert(temp);
45+
}
46+
return;
47+
}
48+
49+
if (left < 0 || right < 0 || open < 0) {
50+
return;
51+
}
52+
53+
std::string current{temp};
54+
char c = s[index];
55+
if (c == '(') {
56+
// Drop
57+
dfs(s, temp, index + 1, left - 1, right, open, result);
58+
// Keep
59+
dfs(s, temp + c, index + 1, left, right, open + 1, result);
60+
} else if (c == ')') {
61+
// Drop
62+
dfs(s, temp, index + 1, left, right - 1, open, result);
63+
// Keep
64+
dfs(s, temp + c, index + 1, left, right, open - 1, result);
65+
} else {
66+
dfs(s, temp + c, index + 1, left, right, open, result);
67+
}
68+
69+
temp = current;
70+
}
71+
72+
std::vector<std::string> remove_invalid_parenthesis(const std::string& s) {
73+
std::vector<std::string> result;
74+
if (s.length() == 0) {
75+
result.push_back("");
76+
return result;
77+
}
78+
int left = 0;
79+
int right = 0;
80+
for (char c : s) {
81+
if (c == '(') left++;
82+
else if (c == ')') {
83+
if (left > 0) left--;
84+
else right++;
85+
}
86+
}
87+
std::unordered_set<std::string> result_set;
88+
std::string temp("");
89+
dfs(s, temp, 0, left, right, 0, result_set);
90+
return std::vector<std::string>(result_set.begin(), result_set.end());
91+
}
92+
93+
template<typename T>
94+
void print_vec(const std::vector<T>& vec) {
95+
for (auto v: vec) {
96+
std::cout << v << " ";
97+
}
98+
std::cout << std::endl;
99+
}
100+
101+
int main()
102+
{
103+
std::string str("(a)())()");
104+
std::cout << "Pattern: " << str << std::endl;
105+
std::cout << "Possible valid parenthesis combinations: ";
106+
print_vec(remove_invalid_parenthesis(str));
107+
}

0 commit comments

Comments
 (0)