Back to course home
0% completed
Vote For New Content
O(N) pqueue python implementation
Pete Stenger
Oct 27, 2024
from heapq import * from collections import Counter import math class Solution: def rearrangeString(self, str1): counts = Counter(str1) heap = [ (-value, char) for char, value in counts.items() ] heapify(heap) if -heap[0][0] > len(str1)//2: return '' res = '_' * len(str1) i = 0 while heap: count, char = heappop(heap) while count < 0: res[i] = char i += 2 if i >= len(res): i = 1 count += 1 return res
0
0
Comments
Comments
P
Pete Stengera year ago
Sorry, that had a typo. from heapq import * from collections import Counter import math class Solution: def rearrangeString(self, str1): counts = Counter(str1) heap = [ (-value, char) for char, value in counts.items() ] heapify(heap) ...