题目
给定一个字符串S,检查是否能重新排布其中的字母,使得两相邻的字符不同。
若可行,输出任意可行的结果。若不可行,返回空字符串。
示例 1:
输入: S = "aab"
输出: "aba"
示例 2:
输入: S = "aaab"
输出: ""
注意:
S 只包含小写字母并且长度在[1, 500]区间内。
解题思路
Heap
不可行的前提条件,字符串中某个字符的出现次数大于字符串长度的一半了。
可行后,每次排字符时,先用出现次数最大的字符和出现次数第二大的字符交错排列,排完后把剩下的字符放到堆里,继续取出来次数最大的和第二大的交错排。
Time complexity:
o
(
n
log
n
)
o(n\log n)
o(nlogn)
Space complexity:
o
(
n
)
o(n)
o(n)
Tricky
Use the most frequent letter to fill as many even indexes as it could, and then use other elements to fill the rest of even indexes. By making sure that the most frequent elements are separate because of even indexes, then other elements can’t be adjacent because we put them on even indexes first, then fill the slots.
Time complexity:
o
(
n
)
o(n)
o(n)
Space complexity:
o
(
n
)
o(n)
o(n)
代码
Heap
class Solution:
def reorganizeString(self, s: str) -> str:
letter_cnt = {}
for i in s:
letter_cnt[i] = letter_cnt.get(i, 0) + 1
if max(letter_cnt.values()) > (len(s) + 1) // 2:
return ''
res = ''
heap = list((-cnt, letter) for letter, cnt in letter_cnt.items())
heapq.heapify(heap)
while len(heap) >= 2:
x1, x2 = heapq.heappop(heap), heapq.heappop(heap)
res += x1[1] + x2[1]
if x1[0] + 1 != 0:
heapq.heappush(heap, (x1[0] + 1, x1[1]))
if x2[0] + 1 != 0:
heapq.heappush(heap, (x2[0] + 1, x2[1]))
if len(heap) > 1:
return ""
if len(heap) == 1:
res += heap[0][1]
return res
Tricky
class Solution:
def reorganizeString(self, s: str) -> str:
letter_cnt = {}
for i in s:
letter_cnt[i] = letter_cnt.get(i, 0) + 1
max_l, max_c = '', 0
for letter, cnter in letter_cnt.items():
if cnter > max_c:
max_c = cnter
max_l = letter
if max_c > (len(s) + 1) // 2:
return ''
res = [''] * len(s)
put_i = 0
while max_c > 0 and put_i < len(s):
res[put_i] = max_l
max_c -= 1
put_i += 2
letter_cnt[max_l] = 0
possible_keys = list(letter_cnt.keys())
key_index = 0
if put_i >= len(res):
put_i = 1
while key_index < len(possible_keys):
while letter_cnt[possible_keys[key_index]] != 0:
res[put_i] = possible_keys[key_index]
letter_cnt[possible_keys[key_index]] -= 1
put_i += 2
if put_i >= len(res):
put_i = 1
key_index += 1
return ''.join(res)