|
44 | 44 |
|
45 | 45 | <!-- 这里可写通用的实现逻辑 -->
|
46 | 46 |
|
| 47 | +**方法一:字符串哈希** |
| 48 | + |
47 | 49 | <!-- tabs:start -->
|
48 | 50 |
|
49 | 51 | ### **Python3**
|
|
59 | 61 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 62 |
|
61 | 63 | ```java
|
| 64 | +class Solution { |
| 65 | + private static final int BASE = 131; |
| 66 | + private static final long[] MUL = new long[310]; |
| 67 | + private static final int MOD = (int) 1e9 + 7; |
| 68 | + static { |
| 69 | + MUL[0] = 1; |
| 70 | + for (int i = 1; i < MUL.length; ++i) { |
| 71 | + MUL[i] = (MUL[i - 1] * BASE) % MOD; |
| 72 | + } |
| 73 | + } |
| 74 | + public List<List<Integer>> palindromePairs(String[] words) { |
| 75 | + int n = words.length; |
| 76 | + long[] prefix = new long[n]; |
| 77 | + long[] suffix = new long[n]; |
| 78 | + for (int i = 0; i < n; ++i) { |
| 79 | + String word = words[i]; |
| 80 | + int m = word.length(); |
| 81 | + for (int j = 0; j < m; ++j) { |
| 82 | + int t = word.charAt(j) - 'a' + 1; |
| 83 | + int s = word.charAt(m - j - 1) - 'a' + 1; |
| 84 | + prefix[i] = (prefix[i] * BASE) % MOD + t; |
| 85 | + suffix[i] = (suffix[i] * BASE) % MOD + s; |
| 86 | + } |
| 87 | + } |
| 88 | + List<List<Integer>> ans = new ArrayList<>(); |
| 89 | + for (int i = 0; i < n; ++i) { |
| 90 | + for (int j = i + 1; j < n; ++j) { |
| 91 | + if (check(i, j, words[j].length(), words[i].length(), prefix, suffix)) { |
| 92 | + ans.add(Arrays.asList(i, j)); |
| 93 | + } |
| 94 | + if (check(j, i, words[i].length(), words[j].length(), prefix, suffix)) { |
| 95 | + ans.add(Arrays.asList(j, i)); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return ans; |
| 100 | + } |
| 101 | + |
| 102 | + private boolean check(int i, int j, int n, int m, long[] prefix, long[] suffix) { |
| 103 | + long t = ((prefix[i] * MUL[n]) % MOD + prefix[j]) % MOD; |
| 104 | + long s = ((suffix[j] * MUL[m]) % MOD + suffix[i]) % MOD; |
| 105 | + return t == s; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
62 | 109 |
|
| 110 | +### **Go** |
| 111 | + |
| 112 | +```go |
| 113 | +func palindromePairs(words []string) [][]int { |
| 114 | + base := 131 |
| 115 | + mod := int(1e9) + 7 |
| 116 | + mul := make([]int, 310) |
| 117 | + mul[0] = 1 |
| 118 | + for i := 1; i < len(mul); i++ { |
| 119 | + mul[i] = (mul[i-1] * base) % mod |
| 120 | + } |
| 121 | + n := len(words) |
| 122 | + prefix := make([]int, n) |
| 123 | + suffix := make([]int, n) |
| 124 | + for i, word := range words { |
| 125 | + m := len(word) |
| 126 | + for j, c := range word { |
| 127 | + t := int(c-'a') + 1 |
| 128 | + s := int(word[m-j-1]-'a') + 1 |
| 129 | + prefix[i] = (prefix[i]*base)%mod + t |
| 130 | + suffix[i] = (suffix[i]*base)%mod + s |
| 131 | + } |
| 132 | + } |
| 133 | + check := func(i, j, n, m int) bool { |
| 134 | + t := ((prefix[i]*mul[n])%mod + prefix[j]) % mod |
| 135 | + s := ((suffix[j]*mul[m])%mod + suffix[i]) % mod |
| 136 | + return t == s |
| 137 | + } |
| 138 | + var ans [][]int |
| 139 | + for i := 0; i < n; i++ { |
| 140 | + for j := i + 1; j < n; j++ { |
| 141 | + if check(i, j, len(words[j]), len(words[i])) { |
| 142 | + ans = append(ans, []int{i, j}) |
| 143 | + } |
| 144 | + if check(j, i, len(words[i]), len(words[j])) { |
| 145 | + ans = append(ans, []int{j, i}) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return ans |
| 150 | +} |
63 | 151 | ```
|
64 | 152 |
|
65 | 153 | ### **...**
|
|
0 commit comments