-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.valid-parentheses.js
46 lines (44 loc) · 1.07 KB
/
20.valid-parentheses.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @lc app=leetcode id=20 lang=javascript
*
* [20] Valid Parentheses
*/
// @lc code=start
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
let stack = []
let chars = {')': '(', ']': '[', '}': '{'}
for (let i=0; i<s.length; i++) {
if (!chars[s[i]])
stack.push(s[i]);
else if (stack.pop() != chars[s[i]])
return false;
}
return !stack.length
};
// @lc code=end
console.log('True:')
console.log(isValid('()'))
console.log(isValid('()()'))
console.log(isValid('()[]'))
console.log(isValid(''))
console.log(isValid('((()))'))
console.log(isValid('({})'))
console.log(isValid('({}[])'))
console.log(isValid('({}[][[]])'))
console.log(isValid('({})'))
console.log('False:')
console.log(isValid('([)]'))
console.log(isValid('()('))
console.log(isValid('())()'))
console.log(isValid('()([]'))
console.log(isValid(')'))
console.log(isValid('('))
console.log(isValid('({[})'))
console.log(isValid('({]})'))
console.log(isValid('(({}[])'))
console.log(isValid('[({}[][[]])'))
console.log(isValid('[[[[[['))