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 e22405b commit 04ac9c3Copy full SHA for 04ac9c3
src/main/java/com/fishercoder/solutions/_20.java
@@ -59,4 +59,26 @@ public boolean isValid(String s) {
59
return stack.isEmpty();
60
}
61
62
+
63
+ public static class Solution2 {
64
+ /**
65
+ * A more concise solution:
66
+ * credit: https://leetcode.com/problems/valid-parentheses/discuss/9178/Short-java-solution
67
+ * */
68
+ public boolean isValid(String s) {
69
+ Stack<Character> stack = new Stack<>();
70
+ for (char c : s.toCharArray()) {
71
+ if (c == '(') {
72
+ stack.push(')');
73
+ } else if (c == '{') {
74
+ stack.push('}');
75
+ } else if (c == '[') {
76
+ stack.push(']');
77
+ } else if (stack.isEmpty() || stack.pop() != c) {
78
+ return false;
79
+ }
80
81
+ return stack.isEmpty();
82
83
84
0 commit comments