File tree 2 files changed +41
-0
lines changed
main/java/com/smlnskgmail/jaman/leetcodejava/medium
test/java/com/smlnskgmail/jaman/leetcodejava/medium
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments