-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-ValidParanthesis.java
44 lines (41 loc) · 1.31 KB
/
20-ValidParanthesis.java
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
38
39
40
41
42
43
44
package LeetCodeProgramsDSA;
import java.util.Stack;
public class Que20 {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
char[] ch = s.toCharArray();
for(char temp : ch){
if(temp=='(' || temp=='{' || temp=='['){
stack.push(temp);
continue;
}else if(stack.isEmpty()){
return false;
}
char top = stack.pop();
if(temp==')' && top!='(')
return false;
else if(temp=='}' && top!='{')
return false;
else if (temp==']' && top!='[')
return false;
}
return stack.isEmpty();
}
public static void main(String[] args) {
Que20 obj = new Que20();
String str = new String("([)]");
System.out.println(obj.isValid(str));
}
}
// Arrays.sort(strs);
// String subSet = "";
// String small = strs[0];
// String big = strs[strs.length - 1];
// for (int i = 0; i < small.length(); i++) {
// if (small.charAt(i) == big.charAt(i)) {
// subSet=subSet+small.charAt(i);
// }
// else
// break;
// }
// return subSet;