|
37 | 37 |
|
38 | 38 | <!-- 这里可写通用的实现逻辑 -->
|
39 | 39 |
|
| 40 | +**方法一:字符串哈希** |
| 41 | + |
| 42 | +问题等价于**找到字符串 s 的最长回文前缀**。 |
| 43 | + |
| 44 | +记 s 的长度为 n,其最长回文前缀的长度为 m,将 s 的后 n-m 个字符反序并添加到 s 的前面即可构成最短回文串。 |
| 45 | + |
40 | 46 | <!-- tabs:start -->
|
41 | 47 |
|
42 | 48 | ### **Python3**
|
43 | 49 |
|
44 | 50 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
45 | 51 |
|
46 | 52 | ```python
|
47 |
| - |
| 53 | +class Solution: |
| 54 | + def shortestPalindrome(self, s: str) -> str: |
| 55 | + base = 131 |
| 56 | + mod = 10**9 + 7 |
| 57 | + n = len(s) |
| 58 | + prefix = suffix = 0 |
| 59 | + mul = 1 |
| 60 | + idx = 0 |
| 61 | + for i, c in enumerate(s): |
| 62 | + prefix = (prefix * base + (ord(c) - ord('a') + 1)) % mod |
| 63 | + suffix = (suffix + (ord(c) - ord('a') + 1) * mul) % mod |
| 64 | + mul = (mul * base) % mod |
| 65 | + if prefix == suffix: |
| 66 | + idx = i + 1 |
| 67 | + return s if idx == n else s[idx:][::-1] + s |
48 | 68 | ```
|
49 | 69 |
|
50 | 70 | ### **Java**
|
51 | 71 |
|
52 | 72 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
53 | 73 |
|
54 | 74 | ```java
|
| 75 | +class Solution { |
| 76 | + public String shortestPalindrome(String s) { |
| 77 | + int base = 131; |
| 78 | + int mul = 1; |
| 79 | + int mod = (int) 1e9 + 7; |
| 80 | + int prefix = 0, suffix = 0; |
| 81 | + int idx = 0; |
| 82 | + int n = s.length(); |
| 83 | + for (int i = 0; i < n; ++i) { |
| 84 | + int t = s.charAt(i) - 'a' + 1; |
| 85 | + prefix = (int) (((long) prefix * base + t) % mod); |
| 86 | + suffix = (int) ((suffix + (long) t * mul) % mod); |
| 87 | + mul = (int) (((long) mul * base) % mod); |
| 88 | + if (prefix == suffix) { |
| 89 | + idx = i + 1; |
| 90 | + } |
| 91 | + } |
| 92 | + if (idx == n) { |
| 93 | + return s; |
| 94 | + } |
| 95 | + return new StringBuilder(s.substring(idx)).reverse().toString() + s; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### **C++** |
| 101 | + |
| 102 | +```cpp |
| 103 | +typedef unsigned long long ull; |
| 104 | + |
| 105 | +class Solution { |
| 106 | +public: |
| 107 | + string shortestPalindrome(string s) { |
| 108 | + int base = 131; |
| 109 | + ull mul = 1; |
| 110 | + ull prefix = 0; |
| 111 | + ull suffix = 0; |
| 112 | + int idx = 0, n = s.size(); |
| 113 | + for (int i = 0; i < n; ++i) |
| 114 | + { |
| 115 | + int t = s[i] - 'a' + 1; |
| 116 | + prefix = prefix * base + t; |
| 117 | + suffix = suffix + mul * t; |
| 118 | + mul *= base; |
| 119 | + if (prefix == suffix) idx = i + 1; |
| 120 | + } |
| 121 | + if (idx == n) return s; |
| 122 | + string x = s.substr(idx, n - idx); |
| 123 | + reverse(x.begin(), x.end()); |
| 124 | + return x + s; |
| 125 | + } |
| 126 | +}; |
| 127 | +``` |
55 | 128 |
|
| 129 | +### **Go** |
| 130 | +
|
| 131 | +```go |
| 132 | +func shortestPalindrome(s string) string { |
| 133 | + n := len(s) |
| 134 | + base, mod := 131, int(1e9)+7 |
| 135 | + prefix, suffix, mul := 0, 0, 1 |
| 136 | + idx := 0 |
| 137 | + for i, c := range s { |
| 138 | + t := int(c-'a') + 1 |
| 139 | + prefix = (prefix*base + t) % mod |
| 140 | + suffix = (suffix + t*mul) % mod |
| 141 | + mul = (mul * base) % mod |
| 142 | + if prefix == suffix { |
| 143 | + idx = i + 1 |
| 144 | + } |
| 145 | + } |
| 146 | + if idx == n { |
| 147 | + return s |
| 148 | + } |
| 149 | + x := []byte(s[idx:]) |
| 150 | + for i, j := 0, len(x)-1; i < j; i, j = i+1, j-1 { |
| 151 | + x[i], x[j] = x[j], x[i] |
| 152 | + } |
| 153 | + return string(x) + s |
| 154 | +} |
56 | 155 | ```
|
57 | 156 |
|
58 | 157 | ### **...**
|
|
0 commit comments