📅  最后修改于: 2023-12-03 14:57:21.660000             🧑  作者: Mango
给定一个字符串s和一个整数k,你需要对字符串进行最小字符替换,使得替换后的字符串能够组成一个k长度的回文字符串。如果无法组成则返回空字符串。
时间复杂度为O(nlogn),其中n为字符串s的长度。统计出每个字符的出现次数需要遍历整个字符串,时间复杂度为O(n),排序的时间复杂度为O(nlogn),替换最多需要遍历k次,每次遍历的时间复杂度为O(n),因此总时间复杂度为O(nlogn + k * n)。
def generate_palindrome(s: str, k: int) -> str:
n = len(s)
if n < k:
return ""
cnt = [0] * 26
for c in s:
cnt[ord(c) - ord('a')] += 1
odd_chars = [chr(i + ord('a')) for i in range(26) if cnt[i] % 2 == 1]
if len(odd_chars) > 1:
return ""
palindrome = [0] * k
mid = k // 2
if k % 2 == 1:
palindrome[mid] = ord(odd_chars[0]) - ord('a')
cnt[palindrome[mid]] -= 1
i, j = mid - 1, mid + 1
else:
i, j = mid - 1, mid
for p in range(26):
c = ord(chr(p + ord('a'))) - ord('a')
while cnt[c] >= 2 and i >= 0 and j < k:
palindrome[i] = c
palindrome[j] = c
cnt[c] -= 2
i -= 1
j += 1
for p in range(26):
c = ord(chr(p + ord('a'))) - ord('a')
while cnt[c] > 0 and i >= 0 and j < k:
palindrome[i] = c
palindrome[j] = c
cnt[c] -= 2
i -= 1
j += 1
if i < 0 and j > k - 1:
return "".join([chr(c + ord('a')) for c in palindrome])
else:
return ""
输入: s = "aabbcc", k = 6 输出: "aabbaa"
输入: s = "abcde", k = 4 输出: ""
输入: s = "abcde", k = 3 输出: "aba"