We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 193f10b commit ac76878Copy full SHA for ac76878
valid-parentheses/TonyKim9401.java
@@ -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