-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhackerRankBalanced Brackets.js
71 lines (62 loc) · 2.19 KB
/
hackerRankBalanced 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
68
69
70
71
// The function is expected to return a BOOLEAN.
// The function accepts STRING string as parameter.
// UPER
// INPUT: STRING - CONTAINS UP TO 4 TYPES OF BRACKETS
// [],(),{},||
// OUTPUT: BOOL
// TRUE IF STRING HAS EQUAL BRACKETS
// FALSE IF STRING HAS MORE BRACKETS ON ONE SIDE OR THE OTHER
// EX
// [(]) => FALSE
// FOO(BAR)BAZ => TRUE
// NO BRACKETS => TRUE
// I (WA)N{T to buy a on}esie[...] b(u{[t] kno}w it) won't suit me. => TRUE
// PLAN
// IDEAS
// USE REG EX, MAYBE WITH A LOOP AND OR COUNTER
// HOW TO TELL IS BALANCED?
// BALANCED
// IF HAS AS MANY OPENING BRACKETS OF A GIVEN TYPE AS CLOSING BRACKETS OF SAME TYPE - BALANCED
// IF BRACKET HAS UNMATCHED BRACKET - UNBALANCED
// OVERLAPPING BRACKET - UNBALANCED - EX: [(])
// WHAT CAN I GOOGLE BESIDES THE EXACT QUESTION?
// regex brackets
// add regex symbols to arr, then arr % 2 - if true return true
// should i do regEx for each direction of each bracket?
// seems like it would work except in else if...
function balancedBrackets(string) {
var bracketBucket = []
// const outputArr = bracketBucket.slice('d', 'o', 'g')
const regExLeftBracket = /\[/g
const regExRightBracket = /\]/g
const regExRightParanth = /\)/g
const regExLeftParanth = /\(/g
const regExVertical = /\[[^\]]*\]/
console.log(string.match('regExVertical', regExVertical))
// console.log(string.match(regExLeftBracket))
// console.log(string.match(regExRightBracket))
// console.log(string.match(regExLeftParanth))
// console.log(string.match(regExRightParanth))
console.log(string.match(regExVertical))
for (var i = 0; i < string.length; i++) {
console.log('i', string.charAt(i))
const char = string.charAt(i)
bracketBucket.push(char)
const regExLeftParanth = /\(/g
console.log(string.match(regExLeftParanth))
// if (char.match(regExRightBracket)) {
// }
}
console.log('bracketBucket:', bracketBucket)
// IF HAS AS MANY OPENING BRACKETS OF A GIVEN TYPE AS CLOSING BRACKETS OF SAME TYPE - BALANCED
if (bracketBucket % 2 === 0) {
// IF BRACKET HAS UNMATCHED BRACKET - UNBALANCED
if (0 === 0) {
return true
} else {
return false
}
}
}
balancedBrackets('[(])') // false
balancedBrackets('[(dog)]') // true