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 7287a87 commit b2a6a85Copy full SHA for b2a6a85
valid-parentheses/Jeehay28.js
@@ -0,0 +1,35 @@
1
+/**
2
+ * @param {string} s
3
+ * @return {boolean}
4
+ */
5
+
6
+// Time Complexity: O(n)
7
+// Space Complexity: O(n)
8
+var isValid = function (s) {
9
+ const obj = {
10
+ "(" : ")",
11
+ "{" : "}",
12
+ "[" : "]",
13
+ };
14
15
+ let stack = [];
16
17
+ for (any of s) {
18
+ // open bracket
19
+ if (obj[any]) {
20
+ stack.push(any);
21
+ } else {
22
+ // close bracket
23
+ if (stack.length === 0) {
24
+ return false;
25
+ } else if (obj[stack[stack.length - 1]] !== any) {
26
27
28
+ stack.pop();
29
+ }
30
31
32
+ return stack.length === 0 ? true : false;
33
+};
34
35
0 commit comments