Skip to content

Commit 1104af6

Browse files
committed
O(n) time and O(1) using Greedy Approach and Max Heap.
1 parent 3196603 commit 1104af6

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

358. Rearrange String k Distance Apart/358. Rearrange String k Distance Apart.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def rearrangeString(self, s: str, k: int) -> str:
3434
"""
3535
if (max_freq)*(k-1) >= len(s) and len(count) > 1:
3636
return ""
37+
"""
38+
In Python, Min heap is created by default. So if we want to build a max heap out of it. We have to take the negative values of the keys.
39+
"""
3740
charFreq = [(-v,k) for k,v in count.items()]
3841
heapq.heapify(charFreq)
3942
while charFreq:

0 commit comments

Comments
 (0)