Skip to content

Commit 079c036

Browse files
committed
checking valid string when placeholder can be used.
1 parent 7740ea9 commit 079c036

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Strings/checkValidString.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public boolean checkValidString(String s) {
3+
int leftBracket = 0;
4+
for (int i=0; i<s.length(); i++) {
5+
if (s.charAt(i) == '(' || s.charAt(i) == '*') {
6+
leftBracket++;
7+
} else {
8+
leftBracket--;
9+
}
10+
if (leftBracket < 0) {
11+
return false;
12+
}
13+
}
14+
15+
if (leftBracket == 0) {
16+
return true;
17+
}
18+
int rightBracket = 0;
19+
for (int i=s.length()-1; i>=0; i--) {
20+
if (s.charAt(i) == ')' || s.charAt(i) == '*') {
21+
rightBracket++;
22+
} else {
23+
rightBracket--;
24+
}
25+
if (rightBracket < 0) {
26+
return false;
27+
}
28+
}
29+
30+
return true;
31+
}
32+
}

0 commit comments

Comments
 (0)