-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (94 loc) · 3.46 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// score: 90
// function getIndexsForPalindrome(word) {
// let res = null
// for (const a of Array.from({ length: word.length }).keys()) {
// for (const b of Array.from({ length: word.length }).keys()) {
// let swapped = [...word]
// let aux = word[a]
// swapped[a] = word[b]
// swapped[b] = aux
// let left = swapped.slice(0, Math.floor(word.length / 2)).join("")
// let right = swapped.slice(Math.ceil(word.length / 2)).reverse().join("")
// res = [
// [
// null
// , [
// []
// , [a, b]
// ][+((a + b) > 0)]
// ][+(left == right)]
// , res
// ][+!!res]
// }
// }
// return res
// }
// score: 100
// function getIndexsForPalindrome(word) {
// const halfLength = Math.floor(word.length / 2);
// const isPalindrome = [...word].slice(0, halfLength).every((letter, i) => {
// return letter === word[word.length - (i + 1)];
// });
// if (isPalindrome) return [];
// for (let i = 0; i <= halfLength; i++) {
// for (let j = 1; j < word.length; j++) {
// const w = [...word];
// [w[i], w[j]] = [w[j], w[i]];
// const isPalindrome = w.slice(0, halfLength).every((letter, i) => {
// return letter === w[w.length - (i + 1)];
// });
// if (isPalindrome) return [i, j];
// }
// }
// return null;
// }
// score: 130
// function getIndexsForPalindrome(word) {
// const wordArray = [...word];
// const wordEntries = wordArray.slice(0, wordArray.length / 2 + 1).entries();
// if (
// wordArray
// .slice(0, wordArray.length / 2)
// .every((c, i) => c === wordArray[wordArray.length - 1 - i])
// )
// return [];
// for (let [i, c1] of wordEntries) {
// for (let [j, c2] of wordArray.entries()) {
// let wordToModify = [...wordArray];
// wordToModify[i] = c2;
// wordToModify[j] = c1;
// if (
// wordToModify
// .slice(0, wordToModify.length / 2)
// .every((c, i) => c === wordToModify[wordToModify.length - 1 - i])
// ) {
// return [i, j];
// }
// }
// }
// return null;
// }
// score: 180
function getIndexsForPalindrome(word) {
const index = word.split('').findIndex((letter, i) => letter != word.at(-i - 1))
const index2 = word.length - 1 - index
if (index == -1) return []
for (let i = index + 1; i < index2; i++) {
const arr = word.split('')
arr[index] = word.at(i)
arr[i] = word.at(index)
if (arr.every((l, i) => l == arr.at(arr.length - 1 - i))) return [index, i]
arr[index] = word.at(index)
arr[i] = word.at(index2)
arr[index2] = word.at(i)
if (arr.every((l, i) => l == arr.at(arr.length - 1 - i))) return [index + i, index2]
}
return null
}
module.exports = getIndexsForPalindrome;
console.log(getIndexsForPalindrome('anna')); // []
console.log(getIndexsForPalindrome('abab')); // [0, 1]
console.log(getIndexsForPalindrome('abac')); // null
console.log(getIndexsForPalindrome('aaaaaaaa')); // []
console.log(getIndexsForPalindrome('aaababa')); // [1, 3]
console.log(getIndexsForPalindrome('caababa')); // null