-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem20.java
More file actions
32 lines (32 loc) · 1.09 KB
/
problem20.java
File metadata and controls
32 lines (32 loc) · 1.09 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
class Solution {
public boolean isValid(String s) {
if(s.length() == 1){
return false;
}
char[] pre = new char[s.length()];
int space = 0;
for(int i = 0 ; i < s.length(); i++){
if(s.charAt(i) == '{' || s.charAt(i) == '(' || s.charAt(i) == '['){
pre[space] = s.charAt(i);
space++;
}
if((s.charAt(i) == '}' && pre[0] == '\0') ||
(s.charAt(i) == ')' && pre[0] == '\0') ||
(s.charAt(i) == ']' && pre[0] == '\0')){
return false;
}else if((s.charAt(i) == '}' && pre[space-1] != '{') ||
(s.charAt(i) == ')' && pre[space-1] != '(') ||
(s.charAt(i) == ']' && pre[space-1] != '[')){
return false;
}else if(s.charAt(i) == '}' || s.charAt(i) == ')' || s.charAt(i) == ']'){
space--;
pre[space] = '\0';
}
System.out.println(pre);
}
if(pre[0] != '\0'){
return false;
}
return true;
}
}