/
index.js
45 lines (36 loc) · 1.24 KB
/
index.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
/**
* @param {string} letter
* @returns boolean
*/
export function isValidUsingTraversing(letter) {
let indexOpenMark = letter.indexOf('(')
let indexCloseMark = letter.indexOf(')')
// eslint-disable-next-line no-constant-condition
while (true) {
if (indexOpenMark === -1 && indexCloseMark === -1) return true // No parenthesis is allowed
if (indexOpenMark === -1 || indexCloseMark === -1) return false // Invalid parenthesis pair
// At this point, both opene & close parenthesis are found
if (indexOpenMark > indexCloseMark) return false
const gift = letter.substring(indexOpenMark + 1, indexCloseMark)
if (gift === '') return false
if (gift.match(/[(){}[\]]/)) return false
indexOpenMark = letter.indexOf('(', indexCloseMark + 1)
indexCloseMark = letter.indexOf(')', indexCloseMark + 1)
}
}
/**
* @param {string} letter
* @returns boolean
*/
export function isValidUsingRegex(letter) {
const gifts = letter.match(/\((.*?)(\)|$)/gm)
if (gifts === null) return true // No parenthesis is allowed
const invalidGift = gifts.find(gift => {
return (
gift.length === 2 ||
gift.slice(-1) !== ')' ||
gift.slice(1, -1).match(/[(){}[\]]/)
)
})
return invalidGift === undefined
}