-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrearrange_characters.py
69 lines (55 loc) · 1.52 KB
/
rearrange_characters.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
class Solution:
def maxFrequency(self, freq):
maxCount = 0
maxChar = ""
for key, value in freq.items():
if value > maxCount:
maxCount = value
maxChar = key
return maxCount, maxChar
def rearrangeString(self, str):
freq = {}
char = []
for ch in str:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
char.append(ch)
size = len(str)
maxCount, maxChar = self.maxFrequency(freq)
if maxCount > (size+1) // 2:
return "-1"
result = [""] * size
index = 0
char.remove(maxChar)
while maxCount:
result[index] += maxChar
maxCount -= 1
index += 2
for ch in char:
while freq[ch] > 0:
if index >= size:
index = 1
result[index] += ch
freq[ch] -= 1
index += 2
return "".join(result)
# Driver Code
if __name__ == '__main__':
t = int(input())
for _ in range(t):
str1 = input()
solObj = Solution()
str2 = solObj.rearrangeString(str1)
if str2 == '-1':
print(0)
elif sorted(str1) != sorted(str2):
print(0)
else:
for i in range(len(str2)-1):
if str2[i] == str2[i+1]:
print(0)
break
else:
print(1)