-
Notifications
You must be signed in to change notification settings - Fork 0
/
678.valid-parenthesis-string.cpp
59 lines (57 loc) · 1.2 KB
/
678.valid-parenthesis-string.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
/*
* @lc app=leetcode id=678 lang=cpp
*
* [678] Valid Parenthesis String
*/
#include <string>
#include <vector>
using namespace std;
// @lc code=start
class Solution
{
public:
bool checkValidString(string str)
{
vector<int> l;
vector<int> s;
for (int i = 0; i < str.length(); i++)
{
char c = str[i];
if (c == '(')
{
l.push_back(i);
}
else if (c == '*')
{
s.push_back(i);
}
else
{
// ')'
if (l.empty() && s.empty())
return false;
if (!l.empty())
{
l.pop_back();
continue;
}
if (!s.empty())
{
s.pop_back();
continue;
}
}
}
while (!l.empty())
{
if (s.empty())
return false;
if (l.back() > s.back())
return false;
l.pop_back();
s.pop_back();
}
return true;
}
};
// @lc code=end