MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / reorganizeString

Method reorganizeString

Python/reorganize-string.py:10–24  ·  view source on GitHub ↗
(self, S: str)

Source from the content-addressed store, hash-verified

8'''
9class Solution:
10 def reorganizeString(self, S: str) -> str:
11 n = len(S)
12 a= []
13
14 for c,x in sorted((S.count(x), x) for x in set(S)):
15 if c > (n+1)/2: # not possible
16 return ''
17 a.extend(c*x)
18
19 # empty list
20 ans = [None] * n
21
22 # placing letters to make possible result
23 ans[::2], ans[1::2] = a[n//2:], a[:n//2]
24 return "".join(ans)
25
26

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected