-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalanced_brackets.js
67 lines (59 loc) · 1.95 KB
/
balanced_brackets.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
### Balanced Brackets
There are 3 kinds of Brackets: [] {} ().
Given a String of characters, check if all the brackets in the String are Balanced.
A string is balanced if all the start and end brackets are in a correct order so they match each other.
*/
function balancedBrackets(string) {
let stringList = string.split('')
let brackets = []
stringList.forEach(letter => {
if (letter.search(/[()]/) != -1 || letter.search(/[\[\]]/) != -1 || letter.search(/[{}]/) != -1) {
brackets.push(letter)
}
})
// if brackets are even check wether they match
if (brackets.length % 2 == 0) {
console.log(brackets)
return bracketMatcher(brackets)
}
return false
}
function bracketMatcher(brackets) {
brackets.forEach((letter, index) => {
// if closer doesnt pair to opener return false
// else pop pair and continue recursion
// if result is empty list it means all pairs are popped successfully
let prevIndex = index - 1
if (letter == ']') {
if (brackets[prevIndex] != '[') { return false }
else {
brackets.splice(prevIndex, 2)
return bracketMatcher(brackets)
}
}
else if (letter == '}') {
if (brackets[prevIndex] != '{') { return false }
else {
brackets.splice(prevIndex, 2)
return bracketMatcher(brackets)
}
}
else if (letter == ')') {
if (brackets[prevIndex] != '(') { return false }
else {
brackets.splice(prevIndex, 2)
return bracketMatcher(brackets)
}
}
})
console.log(brackets)
return brackets.length == 0
}
console.log(balancedBrackets('(hello)[world]'))
// => true
console.log(balancedBrackets('([)]'))
// => false
console.log(balancedBrackets('[({}{}{})([])]'))
// => true
module.exports = balancedBrackets