@@ -35,7 +35,40 @@ def rearrangeString(self, s, k):
35
35
import itertools
36
36
37
37
38
+ # reference: https://codeforces.com/blog/entry/110184 1774B - Coloring
38
39
class Solution2 (object ):
40
+ def rearrangeString (self , s , k ):
41
+ """
42
+ :type str: str
43
+ :type k: int
44
+ :rtype: str
45
+ """
46
+ if not k :
47
+ return s
48
+ cnts = collections .Counter (s )
49
+ bucket_cnt = (len (s )+ k - 1 )// k
50
+ if not (max (cnts .itervalues ()) <= bucket_cnt and cnts .values ().count (bucket_cnt ) <= (len (s )- 1 )% k + 1 ):
51
+ return ""
52
+ result = [0 ]* len (s )
53
+ i = 0
54
+ for c in itertools .chain ((c for c , v in cnts .iteritems () if v == bucket_cnt ),
55
+ (c for c , v in cnts .iteritems () if v <= bucket_cnt - 2 ),
56
+ (c for c , v in cnts .iteritems () if v == bucket_cnt - 1 )):
57
+ for _ in xrange (cnts [c ]):
58
+ result [i ] = c
59
+ i += k
60
+ if i >= len (result ):
61
+ i = i % k + 1
62
+ return "" .join (result )
63
+
64
+
65
+ # Time: O(n)
66
+ # Space: O(n)
67
+ import collections
68
+ import itertools
69
+
70
+
71
+ class Solution3 (object ):
39
72
def rearrangeString (self , s , k ):
40
73
"""
41
74
:type str: str
@@ -61,7 +94,7 @@ def rearrangeString(self, s, k):
61
94
# Space: O(c)
62
95
from collections import Counter
63
96
from heapq import heappush , heappop
64
- class Solution3 (object ):
97
+ class Solution4 (object ):
65
98
def rearrangeString (self , s , k ):
66
99
"""
67
100
:type str: str
0 commit comments