File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
src/com/leetcode/solution Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -49,4 +49,6 @@ public int romanToInt(String s) {
4949
5050
5151 }
52+
53+
5254}
Original file line number Diff line number Diff line change 1+ package com .leetcode .solution ;
2+
3+
4+
5+ import java .util .*;
6+
7+ public class ValidParenthesis {
8+ public static void main (String [] args ) {
9+
10+
11+ String str ="()" ;
12+
13+ Solutions ss = new Solutions ();
14+ boolean b =ss .isValid (str );
15+
16+ System .out .println ("Given parenthesis is: " + b );
17+
18+
19+ }
20+ }
21+ class Solutions {
22+ public boolean isValid (String s ) {
23+
24+
25+ Stack <Character > stack = new Stack <>();
26+ for (char c : s .toCharArray ()) {
27+ if (c == '(' )
28+ stack .push (')' );
29+ else if (c == '{' )
30+ stack .push ('}' );
31+ else if (c == '[' )
32+ stack .push (']' );
33+ else if (stack .isEmpty () || stack .pop () != c )
34+ return false ;
35+ }
36+ return stack .isEmpty ();
37+ }
38+
39+
40+
41+ }
You can’t perform that action at this time.
0 commit comments