-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path358 Rearrange String k Distance Apart.py
88 lines (62 loc) · 1.86 KB
/
358 Rearrange String k Distance Apart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Given a non-empty string str and an integer k, rearrange the string such that the same characters are at least distance
k from each other.
All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string
"".
Example 1:
str = "aabbcc", k = 3
Result: "abcabc"
The same letters are at least distance 3 from each other.
Example 2:
str = "aaabc", k = 3
Answer: ""
It is not possible to rearrange the string.
Example 3:
str = "aaadbbcc", k = 2
Answer: "abacabcd"
Another possible answer is: "abcabcda"
The same letters are at least distance 2 from each other.
Author: Rajeev Ranjan
"""
from collections import defaultdict
import heapq
class Val(object):
def __init__(self, cnt, val):
self.cnt = cnt
self.val = val
def __cmp__(self, other):
if self.cnt == other.cnt:
return cmp(self.val, other.val)
return -cmp(self.cnt, other.cnt)
class Solution(object):
def rearrangeString(self, s, k):
"""
Greedy, largest first, fill k first
O(lg(26) n)
:type s: str
:type k: int
:rtype: str
"""
if not s or k == 0: return s
d = defaultdict(int)
for c in s:
d[c] += 1
h = []
for char, cnt in d.items():
heapq.heappush(h, Val(cnt, char))
ret = []
while h:
cur = []
for _ in xrange(k):
if not h:
return "".join(ret) if len(ret) == len(s) else ""
e = heapq.heappop(h)
ret.append(e.val)
e.cnt -= 1
if e.cnt > 0:
cur.append(e)
for e in cur:
heapq.heappush(h, e)
return "".join(ret)
if __name__ == "__main__":
assert Solution().rearrangeString("aabbccdd", 4) == "abcdabcd"