-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.cpp
65 lines (62 loc) · 1.63 KB
/
Solution.cpp
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
58
59
60
61
62
63
64
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
class Solution {
public:
bool checkValidString(string s) {
int wildcard_cnt = 0;
int stack = 0;
for (int i = 0; i < s.size(); i ++ ) {
switch (s[i]) {
case '(':
stack ++ ;
break;
case ')':
if (stack > 0) stack -- ;
else if (stack == 0) {
if (wildcard_cnt > 0) wildcard_cnt -- ;
else return false;
}
break;
case '*':
wildcard_cnt ++ ;
break;
}
}
if (stack > wildcard_cnt) return false;
stack = wildcard_cnt = 0;
for (int i = s.size() - 1; i >= 0; i -- ) {
switch (s[i]) {
case ')':
stack ++ ;
break;
case '(':
if (stack > 0) stack -- ;
else if (stack == 0) {
if (wildcard_cnt > 0) wildcard_cnt -- ;
else return false;
}
break;
case '*':
wildcard_cnt ++ ;
break;
}
}
return stack <= wildcard_cnt;
}
};
int main() {
Solution a;
bool x = a.checkValidString("(");
cout << x;
return 0;
}