-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge-11.ts
34 lines (26 loc) · 893 Bytes
/
challenge-11.ts
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
export function getIndexsForPalindrome(word: string) {
const _letters = [...word]
const palindrome = word === [..._letters].reverse().join('')
let initial: number[] | null = [null, []][+palindrome]
let index = 0
let aux = 1
const letters = [_letters, []][+palindrome]
let auxLetters = letters.slice(1)
for (const letter of letters) {
for (const auxLetter of auxLetters) {
const w = [...letters]
w[index] = auxLetter
w[aux] = letter
const isPalindrome = +(w.join('') === w.reverse().join(''))
const isInitialNull = +(initial == null)
const isDifferentIndex = +(index !== aux)
const values = [initial, initial, initial, [index, aux]]
initial = values[isInitialNull + isDifferentIndex + isPalindrome]
aux++
}
index++
aux = 1
auxLetters = [[], auxLetters][+(initial == null)]
}
return initial
}