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

Class Solution

Python/reorganize-string.py:9–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7space complexity: O(N)
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