Skip to content

Commit bcc98e0

Browse files
committed
addin algo
1 parent b0965c0 commit bcc98e0

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
class Solution:
4+
def reorganizeString(self, s: str) -> str:
5+
6+
dic_str = dict()
7+
len_s = len(s)
8+
9+
for c in s:
10+
dic_str[c] = dic_str.get(c, 0) + 1
11+
12+
print(dic_str)
13+
14+
lst_s = list(set([c for c in s]))
15+
lst_s.sort(key=lambda x: dic_str[x], reverse=True)
16+
17+
solution = [None for s in s]
18+
19+
if dic_str[lst_s[0]] > (len(s) + 1) // 2:
20+
return ""
21+
22+
i = 0
23+
for c in lst_s:
24+
for _ in range(dic_str[c]):
25+
if i >= len_s:
26+
i = 1
27+
solution[i] = c
28+
i += 2
29+
30+
31+
32+
33+
return ''.join(solution)
34+
35+
36+

0 commit comments

Comments
 (0)