-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid Parentheses.java
More file actions
27 lines (25 loc) · 839 Bytes
/
Copy pathValid Parentheses.java
File metadata and controls
27 lines (25 loc) · 839 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
// Programmed in Java | Author: Anshuman Pratik
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0 ; i < s.length() ; i++) {
char ch = s.charAt(i);
if (ch == '[' || ch == '{' || ch == '(') {
stack.push(ch);
}
else if (!stack.empty() && ch == ')' && stack.peek() == '(') {
stack.pop();
}
else if (!stack.empty() && ch == '}' && stack.peek() == '{') {
stack.pop();
}
else if (!stack.empty() && ch == ']' && stack.peek() == '[') {
stack.pop();
}
else {
return false;
}
}
return stack.empty();
}
}