Skip to content

Commit 75a1e5e

Browse files
authoredSep 8, 2022
2022-09-08 update: added "Valid Parenthesis String" (#87)
1 parent 8bb66af commit 75a1e5e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
// https://leetcode.com/problems/valid-parenthesis-string/
4+
public class ValidParenthesisString {
5+
6+
private final String input;
7+
8+
public ValidParenthesisString(String input) {
9+
this.input = input;
10+
}
11+
12+
public boolean solution() {
13+
int l = 0;
14+
int h = 0;
15+
for (int i = 0; i < input.length(); i++) {
16+
char c = input.charAt(i);
17+
l += c == '(' ? 1 : -1;
18+
h += c != ')' ? 1 : -1;
19+
if (h < 0) {
20+
break;
21+
}
22+
l = Math.max(l, 0);
23+
}
24+
return l == 0;
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class ValidParenthesisStringTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertTrue(new ValidParenthesisString("()").solution());
12+
}
13+
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.