-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
87 lines (87 loc) · 2.2 KB
/
main.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
// /**
// * @param {string[]} words
// * @return {number[][]}
// */
// var palindromePairs = function (words) {
// function isPali (word) {
// let i = 0
// let j = word.length - 1
// while (i <= j && word[i] === word[j]) {
// i++
// j--
// }
// return i > j
// }
// const map = {}
// for (const [index, word] of Object.entries(words)) {
// if (word === '') continue
// if (map[word[0]] == null) map[word[0]] = []
// map[word[0]].push({
// val: word,
// index: +index
// })
// }
// const ans = []
// for (let i = 0; i < words.length; i++) {
// const word = words[i]
// if (word === '') {
// for (let j = 0; j < words.length; j++) {
// if (i !== j && isPali(words[j] + word)) {
// ans.push([j, i])
// if (words[j] !== '') ans.push([i, j])
// }
// }
// continue
// }
// const lastChar = word[word.length - 1]
// if (map[lastChar] == null) continue
// for (const item of map[lastChar]) {
// if (item.index !== i && isPali(item.val + word)) {
// ans.push([item.index, i])
// }
// }
// }
// return ans
// }
// 上面这种方法也是可行的
/**
* @param {string[]} words
* @return {number[][]}
*/
var palindromePairs = function (words) {
function isPali (word) {
let i = 0
let j = word.length - 1
while (i <= j && word[i] === word[j]) {
i++
j--
}
return i > j
}
const res = []
if (words == null || words.length < 2) return res
const map = {}
for (const [i, word] of Object.entries(words)) {
map[word] = i // NOTE: every word is unique
}
for (let [i, word] of Object.entries(words)) {
i = +i
for (let j = 0; j <= word.length; j++) {
const s1 = word.slice(0, j)
const s2 = word.slice(j)
if (isPali(s1)) {
const s2rvs = s2.split('').reverse().join('')
if (map[s2rvs] != null && +map[s2rvs] !== i) {
res.push([+map[s2rvs], i])
}
}
if (s2 !== '' && isPali(s2)) {
const s1rvs = s1.split('').reverse().join('')
if (map[s1rvs] != null && +map[s1rvs] !== i) {
res.push([i, +map[s1rvs]])
}
}
}
}
return res
}