Skip to content

Commit e52d443

Browse files
authored
Add reorganize-string.py & Updated README.md (codedecks-in#154)
1 parent 5cdac14 commit e52d443

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Python/reorganize-string.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
speed: 89.11%
3+
memory: 21:02%
4+
N = len(S), A = 1~26(size of diff alpabet)
5+
ex) aab, N=3, A=2
6+
time complexity: O(A * (N + logA))
7+
space complexity: O(N)
8+
'''
9+
class 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+

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
160160
| 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 | | |
161161
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings/) | [Java](./Java/buddy-strings.java) | _O(n)_ | _O(1)_ | Easy | | |
162162
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Java](./Java/palindrome-number.java) | _O(n)_ | _O(1)_ | Easy | | |
163+
| 767 | [Reorganize String](https://leetcode.com/problems/reorganize-string/) | [Python](./Python/reorganize-string.py) | _O(n)_ | _O(n)_ | Medium | | |
163164
<br/>
164165
<div align="right">
165166
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)