forked from gideo/CodeWars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5kyu_ValidParentheses.js
35 lines (28 loc) · 929 Bytes
/
5kyu_ValidParentheses.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
// 5 kyu - Valid Parentheses
// Description:
// Write a function called validParentheses that takes a string of parentheses, and
// determines if the order of the parentheses is valid. validParentheses should return
// true if the string is valid, and false if it's invalid.
// Examples:
// validParentheses( "()" ) => returns true
// validParentheses( ")(()))" ) => returns false
// validParentheses( "(" ) => returns false
// validParentheses( "(())((()())())" ) => returns true
// All input strings will be nonempty, and will only consist of open parentheses
// '(' and/or closed parentheses ')'
var validParentheses = function(str) {
let arr = [];
for(let i = 0; i < str.length; i+=1) {
if(str[i] === ")") {
if(arr.length <= 0) {
return false;
} else {
arr.shift();
}
}
if(str[i] === "(") {
arr.push(")")
}
}
return arr.length ? false : true;
}