-
Notifications
You must be signed in to change notification settings - Fork 0
/
336.go
115 lines (102 loc) · 2.12 KB
/
336.go
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
110
111
112
113
114
115
package p336
/**
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.
Example 1:
Given words = ["bat", "tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]
Example 2:
Given words = ["abcd", "dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]
*/
type trieNode struct {
word int
children [26]*trieNode
}
type Trie struct {
root *trieNode
}
func (n *trieNode) dfsFind(s []byte) []int {
res := make([]int, 0)
if n.word > 0 && isPalindrome(s) {
res = append(res, n.word-1)
}
for i := byte(0); i < 26; i++ {
if n.children[i] != nil {
res = append(res, n.children[i].dfsFind(append(s, 'a'+i))...)
}
}
return res
}
func (t *Trie) insert(word []byte, ix int) {
cur := t.root
for i := 0; i < len(word); i++ {
v := word[i] - 'a'
if cur.children[v] == nil {
cur.children[v] = new(trieNode)
}
cur = cur.children[v]
}
cur.word = ix
}
func (t *Trie) search(word []byte) []int {
ix := 0
node := t.root
res := make([]int, 0)
for ix < len(word) && node != nil {
if node.word > 0 && isPalindrome(word[ix:]) {
res = append(res, node.word-1)
}
if next := node.children[word[ix]-'a']; next != nil {
ix++
node = next
} else {
node = nil
break
}
}
if ix == len(word) && node != nil {
res = append(res, node.dfsFind([]byte{})...)
}
return res
}
func isPalindrome(w []byte) bool {
i, j := 0, len(w)-1
for i <= j {
if w[i] == w[j] {
i++
j--
} else {
break
}
}
return i > j
}
func reverse(s string) []byte {
res := make([]byte, len(s))
i, j := 0, len(s)-1
for i <= j {
res[i] = s[j]
res[j] = s[i]
i++
j--
}
return res
}
func palindromePairs(words []string) [][]int {
trie := Trie{root: &trieNode{}}
for i, v := range words {
trie.insert(reverse(v), i+1)
}
res := make([][]int, 0)
for i, v := range words {
pairs := trie.search([]byte(v))
for _, p := range pairs {
if p != i {
res = append(res, []int{i, p})
}
}
}
return res
}