-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParentheses.js
More file actions
63 lines (59 loc) · 1.9 KB
/
Copy pathValidParentheses.js
File metadata and controls
63 lines (59 loc) · 1.9 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Name: ValidParentheses.js
//Date: 5/3/2022
//Auth: Clay Stuart
//Description: Create Parentheses checker: '()[]{}'
/*Constraints: Input string length: 1..10,000 char long and is only '()[]{}'
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
*/
/* Pseudocode:
* Create empty array named stack to act as LIFO stack
* Iterate over s
* Whenever an open bracket is encountered in s, push a matching close bracket onto stack
* Whenever a close bracket is encountered in s, push stack and compare.
* If non-match occurs before iteration completes, return false; otherwise, return true
/**
* @param {string} s
* @return {boolean}
*/
//Results: Runtime: 61 ms, faster than 90.18% of JavaScript online submissions for Valid Parentheses.
//Results: Memory Usage: 43.3 MB, less than 15.31% of JavaScript online submissions for Valid Parentheses.
var isValid = function (s) {
if (s.length <= 1 || s.length > 10000) return false;
let stack = [];
let compare = new Map([
['(', ')'],
['{', '}'],
['[', ']'],
])
for (let letter of s) {
if (['(', '{', '['].includes(letter))
stack.push(compare.get(letter)); //push matching bracket onto stack
else if ([')', '}', ']'].includes(letter)) {
let temp = stack.pop();
if (letter == temp)
continue;
else
return false;
}
else
return false;
}
if (stack.length > 0)
return false;
return true;
};
//Simple Test Framework
new Map([
["()", true],
["()[]{}", true],
["(]", false],
["[", false],
["((", false],
]).forEach( function(value, key) {
if (isValid(key) === value)
console.log(`Pass: ${key} returns ${value}`)
else
console.log(`Fail: ${key} does not return ${value}`)
});