-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-parentheses.js
37 lines (28 loc) · 1.19 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
36
37
/**
* https://www.codewars.com/kata/52774a314c2333f0a7000688
*
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 ')'
*
*/
let tests = require('./lib/framework.js');
let Test = tests.Test, describe = tests.describe, it = tests.it, before = tests.before, after = tests.after;
function validParentheses(parens){
var valid = 0;
var arr = parens.split('');
arr.forEach(function (e) {
valid += e === '(' ? 1 : -1;
});
return valid === 0 && arr[0] === '(' && arr[arr.length - 1] === ')';
}
Test.assertEquals(validParentheses( "()" ), true);
Test.assertEquals(validParentheses( "())" ), false);
Test.assertEquals(validParentheses( "(())((()())())" ), true);