-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalid-parentheses.js
executable file
·36 lines (24 loc) · 1.24 KB
/
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
/*https://leetcode.com/problems/valid-parentheses/description/
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.*/
/*Solution Algo -
A) If I can match all of the passed-in parenthesis after traversing the string character by character, then return true. Else, if a single unmatched parenthesis remains, then return false.
B> So, while, traversing the array, create an empty temporaray array to which I will push only unmatched parenthesis, which will be compared for the next iteraion.
C> and all matched parenthesis are popped.
*/
var isValid = function(s) {
var tempArray = [];
s.split('').forEach(function(currentParenthesis) {
var lastUnmatchedParenthesis = tempArray[tempArray.length - 1];
if ( lastUnmatchedParenthesis === "(" && currentParenthesis === ")" ||
lastUnmatchedParenthesis === "{" && currentParenthesis === "}" ||
lastUnmatchedParenthesis === "[" && currentParenthesis === "]" ) {
return tempArray.pop();
} else {
tempArray.push(currentParenthesis);
}
});
return tempArray.length === 0;
}
var str = "(){}[]["
console.log(isValid((str)));