-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valid Parenthesis String
45 lines (45 loc) · 1.04 KB
/
Valid Parenthesis String
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
class Solution {
public:
bool checkValidString(string s) {
stack <int> open ;
stack <int> star ;
for(int i = 0 ; i < s.size() ; i++)
{
if(s[i] == '(')
{
open.push(i) ;
}
else if(s[i] == '*')
{
star.push(i) ;
}
else{
if(!open.empty())
{
open.pop() ;
}
else if(!star.empty() && star.top() < i)
{
star.pop() ;
}
else
{
return false ;
}
}
}
if(open.size() != 0)
{
while(!open.empty())
{
int o = open.top() ;
open.pop() ;
if(star.empty() || star.top() < o )
return false ;
else
star.pop() ;
}
}
return true ;
}
};