-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution678.cs
57 lines (50 loc) · 1.31 KB
/
Solution678.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
57
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution678
{
/// <summary>
/// 678. Valid Parenthesis String - Medium
/// <a href="https://leetcode.com/problems/valid-parenthesis-string">See the problem</a>
/// </summary>
public bool CheckValidString(string s)
{
var leftStack = new Stack<int>();
var starStack = new Stack<int>();
for (var i = 0; i < s.Length; i++)
{
if (s[i] == '(')
{
leftStack.Push(i);
}
else if (s[i] == '*')
{
starStack.Push(i);
}
else
{
if (leftStack.Count == 0 && starStack.Count == 0)
{
return false;
}
if (leftStack.Count > 0)
{
leftStack.Pop();
}
else
{
starStack.Pop();
}
}
}
while (leftStack.Count > 0 && starStack.Count > 0)
{
if (leftStack.Peek() > starStack.Peek())
{
return false;
}
leftStack.Pop();
starStack.Pop();
}
return leftStack.Count == 0;
}
}