-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisBalanced.js
More file actions
46 lines (42 loc) · 910 Bytes
/
Copy pathisBalanced.js
File metadata and controls
46 lines (42 loc) · 910 Bytes
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
// https://www.hackerrank.com/challenges/balanced-brackets/problem
function isBalanced(str) {
const allBrackets = []
const map = {
'{': '}',
'(': ')',
'[': ']',
}
str = str.replace(new RegExp('[0-9]', 'g'), '')
str = str.replace(new RegExp('[a-z A-Z]', 'g'), '')
for (let x = 0; x < str.length; x++) {
const char = str[x]
if (char === '[' || char === '(' || char === '{') {
allBrackets.push(char)
} else {
const closeBracket = allBrackets.pop()
if (char !== map[closeBracket]) {
console.log(map[closeBracket])
return 'NO'
}
}
}
if (allBrackets.length !== 0) {
return 'NO'
}
return 'YES'
}
console.log(isBalanced('f(e(d))'))
/**
*
{
'(a[0]+b[2c[6]]) {24 + 53}' : true,
'f(e(d))' : true,
'[()]{}([])' : true,
'((b)' : false,
'(c]' : false,
'{(a[])' : false,
'([)]' : false,
')(' : false,
'' : false
}
*/