File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -22,3 +22,31 @@ public boolean isValid(String s) {
22
22
return stack .isEmpty ();
23
23
}
24
24
}
25
+
26
+ //Solution with HashMap Lookup table as described in the video
27
+
28
+ class Solution {
29
+ public boolean isValid (String s ) {
30
+ Stack <Character > brackets = new Stack <>();
31
+ Map <Character , Character > bracketLookup = new HashMap <>();
32
+
33
+ bracketLookup .put (')' , '(' );
34
+ bracketLookup .put ('}' , '{' );
35
+ bracketLookup .put (']' , '[' );
36
+
37
+ for (char c : s .toCharArray ()) {
38
+ if (bracketLookup .containsKey (c )) {
39
+ if (brackets .size () != 0 && brackets .peek () == bracketLookup .get (c )) {
40
+ brackets .pop ();
41
+ } else {
42
+ return false ;
43
+ }
44
+ } else {
45
+ brackets .push (c );
46
+ }
47
+ }
48
+
49
+ if (brackets .size () == 0 ) return true ;
50
+ return false ;
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments