Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Python/reorganize-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
speed: 89.11%
memory: 21:02%
N = len(S), A = 1~26(size of diff alpabet)
ex) aab, N=3, A=2
time complexity: O(A * (N + logA))
space complexity: O(N)
'''
class Solution:
def reorganizeString(self, S: str) -> str:
n = len(S)
a= []

for c,x in sorted((S.count(x), x) for x in set(S)):
if c > (n+1)/2: # not possible
return ''
a.extend(c*x)

# empty list
ans = [None] * n

# placing letters to make possible result
ans[::2], ans[1::2] = a[n//2:], a[:n//2]
return "".join(ans)


1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Java](./Java/generate-a-string-with-characters-that-have-odd-counts.java) | _O(n)_ | _O(1)_ | Easy | | |
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | |
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | |
| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | |
<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down