Skip to content

Commit db79894

Browse files
authored
Create _2116.cpp
Solution for the Leetcode problem 2116. Check if a Parentheses String Can Be Valid
1 parent e3f477d commit db79894

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: cpp/_2116.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
bool canBeValid(string s, string locked) {
4+
int n = s.size();
5+
for(int i = 0;i<n;i++)
6+
{
7+
if(locked[i]=='0')
8+
s[i] = '*';
9+
}
10+
cout<<s<<endl;
11+
stack<int> open,star;
12+
for(int i = 0;i<n;i++)
13+
{
14+
if(s[i]=='(') open.push(i);
15+
else if(s[i]=='*') star.push(i);
16+
else if(s[i]==')')
17+
{
18+
if(!open.empty()) open.pop();
19+
else if(!star.empty()) star.pop();
20+
else return false;
21+
}
22+
}
23+
while(!open.empty() && !star.empty() && open.top() < star.top())
24+
{
25+
open.pop();
26+
star.pop();
27+
}
28+
if(star.size()%2)
29+
return false;
30+
return (open.empty());
31+
}
32+
};

0 commit comments

Comments
 (0)