-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0020_valid_parentheses.java
More file actions
37 lines (37 loc) · 1.16 KB
/
Copy path0020_valid_parentheses.java
File metadata and controls
37 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public boolean isValid(String s) {
String arr[] = s.split("");
Stack<String> stack = new Stack<>();
stack.add(arr[0]);
if(s.length() % 2 == 1)
return false;
if(s.length() == 0)
return true;
for(int i = 1; i < s.length(); i++) {
if(arr[i].equals("(") || arr[i].equals("[") || arr[i].equals("{")) {
stack.add(arr[i]);
} else {
String s1 = stack.pop();
//System.out.println(s1);
if(!s1.equals("(") && arr[i].equals(")")) {
System.out.println("1");
return false;
}
if(!s1.equals("{") && arr[i].equals("}")) {
System.out.println("2");
return false;
}
if(!s1.equals("[") && arr[i].equals("]")) {
System.out.println("3");
return false;
}
}
}
if(!stack.isEmpty()) {
System.out.println("4");
return false;
}
else
return true;
}
}