File tree Expand file tree Collapse file tree 2 files changed +31
-41
lines changed
Expand file tree Collapse file tree 2 files changed +31
-41
lines changed Original file line number Diff line number Diff line change 33import java .util .Stack ;
44
55public class IsValid {
6+ public boolean isValid1 (String s ) {
7+ if (s == null || s .length () == 0 ) {
8+ return true ;
9+ }
10+
11+ Stack <Character > stack = new Stack <Character >();
12+ int len = s .length ();
13+ for (int i = 0 ; i < len ; i ++) {
14+ char c = s .charAt (i );
15+ if (stack .isEmpty ()) {
16+ if (c == ']' || c == ')' || c == '}' ) {
17+ return false ;
18+ }
19+ stack .push (c );
20+ continue ;
21+ }
22+
23+ if (c == ')' && stack .peek () == '('
24+ || c == ']' && stack .peek () == '['
25+ || c == '}' && stack .peek () == '{'
26+ ) {
27+ stack .pop ();
28+ } else if (c == '(' || c == '[' || c == '{' ) {
29+ stack .push (c );
30+ } else {
31+ return false ;
32+ }
33+ }
34+
35+ return stack .isEmpty ();
36+ }
637 public boolean isValid (String s ) {
738 if (s == null ) {
839 return false ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments