-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ 20-Valid Parentheses.java
More file actions
28 lines (28 loc) · 1.2 KB
/
Copy pathQ 20-Valid Parentheses.java
File metadata and controls
28 lines (28 loc) · 1.2 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
//Teh logic behind the code is that we create a stack and put the char into the stack if it is a opening bracket.
//when the current index element is a closing bracket then we have to pop the stack element and check if the popped char is correct opening bracket match for the
//current found closing bracket if not then simply return false
public boolean isValid(String s) {
ArrayDeque<Character> stack = new ArrayDeque<>();
for (char e : s.toCharArray()) {
if (e == '{' || e == '[' || e == '(') {
stack.push(e);
continue;
}
if (e == '}' || e == ']' || e == ')')
if (stack.isEmpty())
return false;
char a;
switch (e) {
case '}' : a = stack.pop();
if (a != '{') return false;
break;
case ']' : a = stack.pop();
if (a != '[') return false;
break;
case ')' : a = stack.pop();
if (a != '(') return false;
break;
}
}
return stack.isEmpty();
}