Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9c2fb05

Browse files
authored
Create 2116. Check if a Parentheses String Can Be Valid
1 parent c478889 commit 9c2fb05

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public boolean canBeValid(String s, String locked) {
3+
if(s.length()%2!=0) return false;
4+
Stack<Integer> unlocked = new Stack<>();
5+
Stack<Integer> open = new Stack<>();
6+
char[] stArray = s.toCharArray();
7+
char[] lArray = locked.toCharArray();
8+
9+
for(int i=0;i<lArray.length;i++){
10+
if(lArray[i]=='0'){
11+
unlocked.push(i);
12+
}
13+
else if(stArray[i]=='('){
14+
open.push(i);
15+
}
16+
else{
17+
if(open.size()>0){
18+
open.pop();
19+
}
20+
else if(unlocked.size()>0){
21+
unlocked.pop();
22+
}
23+
else{
24+
return false;
25+
}
26+
}
27+
}
28+
29+
while(open.size()>0 && unlocked.size()>0 && open.peek()<unlocked.peek()){
30+
open.pop();
31+
unlocked.pop();
32+
}
33+
34+
return open.size()==0;
35+
}
36+
}

0 commit comments

Comments
 (0)