Skip to content

Commit ac76878

Browse files
committed
Valid Parentheses
1 parent 193f10b commit ac76878

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

valid-parentheses/TonyKim9401.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// TC: O(n)
2+
// -> n = s.length
3+
// SC: O(n)
4+
// -> n = s.length / 2
5+
class Solution {
6+
public boolean isValid(String s) {
7+
Stack<Character> stack = new Stack<>();
8+
9+
for (char c : s.toCharArray()) {
10+
if (c == '(') stack.add(')');
11+
else if (c == '{') stack.add('}');
12+
else if (c == '[') stack.add(']');
13+
else if (stack.isEmpty() || stack.pop() != c) return false;
14+
}
15+
16+
return stack.isEmpty();
17+
}
18+
}

0 commit comments

Comments
 (0)