-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0301-RemoveInvalidParentheses.cs
56 lines (48 loc) · 2.23 KB
/
0301-RemoveInvalidParentheses.cs
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
//-----------------------------------------------------------------------------
// Runtime: 240ms
// Memory Usage: 31.6 MB
// Link: https://leetcode.com/submissions/detail/379053111/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0301_RemoveInvalidParentheses
{
public IList<string> RemoveInvalidParentheses(string s)
{
int invalidLeft = 0, invalidRight = 0;
foreach (var ch in s)
{
if (ch == '(') invalidLeft++;
else if (ch == ')')
{
invalidRight = invalidLeft == 0 ? invalidRight + 1 : invalidRight;
invalidLeft = invalidLeft == 0 ? invalidLeft : invalidLeft - 1;
}
}
var result = new HashSet<string>();
RemoveInvalidParentheses(s, 0, 0, 0, invalidLeft, invalidRight, new StringBuilder(), result);
return new List<string>(result);
}
private void RemoveInvalidParentheses(string s, int index, int leftCount, int rightCount, int invalidLeft, int invalidRight, StringBuilder sb, HashSet<string> result)
{
if (s.Length == index)
{
if (invalidLeft == 0 && invalidRight == 0) result.Add(sb.ToString());
return;
}
var ch = s[index];
if ((ch == '(' && invalidLeft > 0) || (ch == ')' && invalidRight > 0))
RemoveInvalidParentheses(s, index + 1, leftCount, rightCount, invalidLeft - (ch == '(' ? 1 : 0), invalidRight - (ch == ')' ? 1 : 0), sb, result);
sb.Append(ch);
if (ch != '(' && ch != ')')
RemoveInvalidParentheses(s, index + 1, leftCount, rightCount, invalidLeft, invalidRight, sb, result);
else if (ch == '(')
RemoveInvalidParentheses(s, index + 1, leftCount + 1, rightCount, invalidLeft, invalidRight, sb, result);
else if (rightCount < leftCount)
RemoveInvalidParentheses(s, index + 1, leftCount, rightCount + 1, invalidLeft, invalidRight, sb, result);
sb.Remove(sb.Length - 1, 1);
}
}
}