-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckValidString.cpp
More file actions
41 lines (37 loc) · 937 Bytes
/
checkValidString.cpp
File metadata and controls
41 lines (37 loc) · 937 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
39
40
41
#include<iostream>
#include<string>
#include<stack>
#include<vector>
using namespace std;
bool checkValidString(string s) {
stack<int> stk;
stack<int> index; //记录 * 的位置
for(int i = 0; i < s.size(); ++i){
if(s[i] == '('){
stk.push(i);
}else if(s[i] == '*'){
index.push(i);
}else{
if(!stk.empty())
stk.pop();
else if (index.size() != 0)
index.pop();
else
return false;
}
}
while(!stk.empty() && ! index.empty())
{
if(stk.top() < index.top()){
stk.pop();
index.pop();
}else{
return false;
}
}
cout << stk.size() << endl;
return stk.empty();
}
int main(){
return 0;
}