-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid-parenthesis-string.cpp
More file actions
38 lines (34 loc) · 999 Bytes
/
valid-parenthesis-string.cpp
File metadata and controls
38 lines (34 loc) · 999 Bytes
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
class Solution {
public:
// https://www.cnblogs.com/grandyang/p/7617017.html 中的法2
bool checkValidString(string s) {
int c=0;
for(int i=0;i<s.size();i++){
if(s[i]=='(' || s[i]=='*') c++;
else c--;
if(c<0) return false;
}
if(c==0) return true;
c=0;
for(int i=s.size()-1;i>=0;i--){
if(s[i]==')' || s[i]=='*') c++;
else c--;
if(c<0) return false;
}
return true;
}
// 自己写的,暴力dfs
bool checkValidString1(string s) {
return dfs(s,0,0,0);
}
bool dfs(string &s, int l, int r, int i){
if(i==s.size() ){
if(l==r) return true;
else return false;
}
if(l<r) return false;
if(s[i]=='(') return dfs(s,l+1,r,i+1);
if(s[i]==')') return dfs(s,l,r+1,i+1);
else return dfs(s,l+1,r,i+1) || dfs(s,l,r+1,i+1) || dfs(s,l,r,i+1);
}
};