-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathValidParantheses.java
More file actions
28 lines (27 loc) · 841 Bytes
/
ValidParantheses.java
File metadata and controls
28 lines (27 loc) · 841 Bytes
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
class ValidParantheses {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray()){
if(c == '{' || c == '[' || c == '('){
stack.push(c);
}else{
if(stack.isEmpty()){
return false;
}else{
char top = stack.peek();
if(c == '}' && top == '{' ||
c == ')' && top =='(' ||
c == ']' && top == '['){
stack.pop();
}else{
return false;
}
}
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
System.out.println(isValid("()[]{}"));
}
}