Skip to content

Commit 7feb899

Browse files
Create 678. Valid Parenthesis String.cpp
The most optimal solution of this problem on leetcode
1 parent dfa8012 commit 7feb899

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

678. Valid Parenthesis String.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean checkValidString(String s) {
3+
int min=0,max=0;
4+
for(int i=0;i<s.length();i++){
5+
if(s.charAt(i)=='('){
6+
min++;
7+
max++;
8+
}else if(s.charAt(i)==')'){
9+
min--;
10+
max--;
11+
}else{
12+
min--;
13+
max++;
14+
}
15+
if(min<0) min = 0;
16+
if(max<0) return false;
17+
}
18+
return min==0;
19+
}
20+
}

0 commit comments

Comments
 (0)