-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_parentheses.js
41 lines (30 loc) · 979 Bytes
/
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
/// Write a function that takes a string of parentheses, and determines
/// if the order of the parentheses is valid. The function should
/// return true if the string is valid, and false if it's invalid.
// string is valid if:
// - equal num of opening and closign parens
// - check each open paren closes
// -- traverse chars, if closing paren then good, if open paren then check if that one closes before continuing
// -- use recursion
// - (()()(
function validParentheses(parens) {
let arr = parens.split('');
// can't start with closing tag
if (arr[0] !== '(') return false;
arr.forEach((c, idx) => {
findCloseParen(c, arr, idx);
})
return isValid;
}
function findCloseParen(start, arr, idx) {
if (start !== '(') return "not an opening tag";
let searchArr = arr;
searchArr.splice(0, idx + 1);
searchArr.forEach(c => {
if (c == ")") {
} else if (c == "(") {
findCloseParen(c, arr);
}
})
return indexOfClosingParen;
}