-
Notifications
You must be signed in to change notification settings - Fork 0
/
kata.js
52 lines (43 loc) · 1.5 KB
/
kata.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
// #1
// function validBraces(braces) {
// let open = ['(', '[', '{'];
// let close = [')', ']', '}'];
// let arr = [];
// for (let i = 0; i < braces.length; i++){
// if (open.includes(braces[i])) {
// arr.push(braces[i]);
// } else {
// if (close.indexOf(braces[i]) === open.indexOf(arr[arr.length-1])){
// arr.pop()
// } else {
// return false;
// }
// }
// }
// return arr.length === 0;
// }
// # 2
// function validBraces(braces){
// while (/\(\)|\[\]|\{\}/g.test(braces)) {
// braces = braces.replace(/\(\)|\[\]|\{\}/g,"")
// }
// return !braces.length;
// }
// #3
const validBraces = (b) => /\(\)|\{\}|\[\]/.test(b) ? validBraces(b.replace(/\(\)|\{\}|\[\]/, '')) : '' === b;
console.log(validBraces("()))")); // false
console.log(validBraces("()")); // true
console.log(validBraces("[]")); // true
console.log(validBraces("{}")); // true
console.log(validBraces("(){}[]")); // true
console.log(validBraces("([{}])")); // true
console.log(validBraces("(}")); // false
console.log(validBraces("[(])")); // false
console.log(validBraces("({})[({})]")); // true
console.log(validBraces("(})")); // false
console.log(validBraces("(({{[[]]}}))")); // true
console.log(validBraces("{}({})[]")); // true
console.log(validBraces(")(}{][")); // false
console.log(validBraces("())({}}{()][][")); // false
console.log(validBraces("(((({{")); // false
console.log(validBraces("}}]]))}])")); // false