File tree Expand file tree Collapse file tree 1 file changed +5
-5
lines changed Expand file tree Collapse file tree 1 file changed +5
-5
lines changed Original file line number Diff line number Diff line change @@ -28,15 +28,16 @@ public boolean isValid(String s) {
28
28
class Solution {
29
29
public boolean isValid (String s ) {
30
30
Stack <Character > brackets = new Stack <>();
31
- Map <Character , Character > bracketLookup = new HashMap <>();
31
+ Map <Character , Character > bracketLookup = new HashMap <>(3 );
32
32
33
33
bracketLookup .put (')' , '(' );
34
34
bracketLookup .put ('}' , '{' );
35
35
bracketLookup .put (']' , '[' );
36
36
37
- for (char c : s .toCharArray ()) {
37
+ for (int i = 0 ; i < s .length (); i ++) {
38
+ char c = s .charAt (i );
38
39
if (bracketLookup .containsKey (c )) {
39
- if (brackets .size () != 0 && brackets . peek () == bracketLookup .get (c )) {
40
+ if (! brackets .isEmpty () && bracketLookup .get (c ). equals ( brackets . peek () )) {
40
41
brackets .pop ();
41
42
} else {
42
43
return false ;
@@ -46,7 +47,6 @@ public boolean isValid(String s) {
46
47
}
47
48
}
48
49
49
- if (brackets .size () == 0 ) return true ;
50
- return false ;
50
+ return brackets .isEmpty ();
51
51
}
52
52
}
You can’t perform that action at this time.
0 commit comments