Skip to content

Commit a0debab

Browse files
author
Partho Biswas
committed
767. Reorganize String
1 parent 9b060dd commit a0debab

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ I have solved quite a number of problems from several topics. See the below tabl
225225
| --- | --- | --- | --- | --- | --- |
226226
|01| [253_Meeting_Rooms_II](https://leetcode.com/problems/meeting-rooms-ii/)| [Python](leetcode.com/python/253_Meeting_Rooms_II.py)| [Official](https://leetcode.com/problems/meeting-rooms-ii/solution/) | Medium | --- |
227227
|02| [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/solution/)| [Python](leetcode.com/python/347_Top_K_Frequent_Elements.py)| [Helper 1](https://stackoverflow.com/questions/19979518/what-is-pythons-heapq-module), [Helper 2](https://stackoverflow.com/a/12373856/2089253), [heapq](https://docs.python.org/3.0/library/heapq.html), [Counter](https://docs.python.org/3/library/collections.html#collections.Counter) | Medium | --- |
228+
|03| [767. Reorganize String](https://leetcode.com/problems/reorganize-string/)| [Python](leetcode.com/python/767_Reorganize_String.py)| --- | Medium | Also check Greedy approach |
228229

229230

230231
### [Tries](https://leetcode.com/explore/learn/card/trie/)
@@ -284,6 +285,7 @@ I have solved quite a number of problems from several topics. See the below tabl
284285
|06| [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/)| [Python](leetcode.com/python/392_Is_Subsequence.py)| [Ex 1](https://leetcode.com/problems/is-subsequence/discuss/87264/Easy-to-understand-binary-search-solution), [Ex 3](https://leetcode.com/problems/is-subsequence/discuss/87302/Binary-search-solution-for-follow-up-with-detailed-comments/144323) | Easy | 📌 |
285286
|07| **[55. Jump Game](https://leetcode.com/problems/jump-game/)** | [Python](leetcode.com/python/55_Jump_Game.py)| **[Official](https://leetcode.com/articles/jump-game/)** , [Art 1](https://tinyurl.com/yy9vyjyn)| Medium | 📌 **Must Check. Learned a lot** |
286287
|08| [45. Jump Game II](https://leetcode.com/problems/jump-game-ii/)| [Python](leetcode.com/python/45_Jump_Game_II.py)| [Vid 1](https://www.youtube.com/watch?v=cETfFsSTGJI), [Vid 2](https://www.youtube.com/watch?v=jH_5ypQggWg), [Vid 3](https://www.youtube.com/watch?v=vBdo7wtwlXs), [Art 1](https://tinyurl.com/yalsno6r) | Hard | 📌 |
288+
|09| [767. Reorganize String](https://leetcode.com/problems/reorganize-string/)| [Python](leetcode.com/python/767_Reorganize_String.py)| --- | Medium | Also check Heap approach |
287289

288290

289291
### [Dynamic Programming](https://www.educative.io/courses/grokking-dynamic-programming-patterns-for-coding-interviews)
@@ -504,6 +506,11 @@ Some helpful links, channels, tutorials, blogs.
504506
05. [The 30-minute guide to rocking your next coding interview](https://tinyurl.com/vbqlhko)
505507
06. [Google Interview Part-1](https://tinyurl.com/w62ou5j) , [Google Interview Part-2](https://tinyurl.com/u69s6pe), [Google Interview Part-3](https://tinyurl.com/urv95zu)
506508
07. [Amazon ও Google এ চাকরির সুযোগ পাওয়ার প্রস্তুতি পর্ব](https://github.com/sabir4063/my_preparation)
509+
08. [How to use LeetCode effectively](https://www.youtube.com/watch?v=iGFnsuMeUQY)
510+
09. [How to solve ANY interview question](https://www.youtube.com/watch?v=JB78__e1tlE&t=3s)
511+
10. [How To Ace The Google Coding Interview - Complete Guide](https://tinyurl.com/ws2d4cr)
512+
11. [Software Engineering Interviews: The Golden Path To Passing](https://tinyurl.com/vb77j83)
513+
12. [A MUST READ](https://tinyurl.com/vzbn34s)
507514

508515

509516
<b>Courses/Books</b>
@@ -605,7 +612,6 @@ Learn the following modules by heart. Just knowing all of the following items wi
605612
06. [Thinking Recursively in Python](https://realpython.com/python-thinking-recursively/)
606613
07. [Breaking out of a recursive function?](https://stackoverflow.com/questions/10544513/breaking-out-of-a-recursive-function)
607614
08. [Why does recursion return the first call in the stack and not the last?](https://softwareengineering.stackexchange.com/questions/333247/why-does-recursion-return-the-first-call-in-the-stack-and-not-the-last)
608-
09. []()
609615

610616

611617

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from collections import Counter
2+
import heapq
3+
4+
class Solution(object):
5+
6+
# # Using Heap/PriorityQueue. Original code from here: https://tinyurl.com/u6h7nmd
7+
# # ALso this explanation: https://leetcode.com/problems/reorganize-string/discuss/113457/Simple-python-solution-using-PriorityQueue/194973
8+
# def reorganizeString(self, S):
9+
# """
10+
# :type S: str
11+
# :rtype: str
12+
# """
13+
# result = ""
14+
# priorityQueue = []
15+
# charFrequencies = Counter(S)
16+
# for key, value in charFrequencies.items():
17+
# heapq.heappush(priorityQueue, (-value, key))
18+
# prevNodeValue, prevNodeKey = 0, ''
19+
# while priorityQueue:
20+
# currentNodeValue, currentNodeKey = heapq.heappop(priorityQueue)
21+
# result += currentNodeKey
22+
# if prevNodeValue < 0:
23+
# heapq.heappush(priorityQueue, (prevNodeValue, prevNodeKey))
24+
# currentNodeValue += 1
25+
# prevNodeValue, prevNodeKey = currentNodeValue, currentNodeKey
26+
# return "" if len(result) != len(S) else result
27+
28+
29+
# Greedy solution. Original code from here: https://tinyurl.com/rnsr4gp
30+
def reorganizeString(self, S):
31+
a = sorted(sorted(S), key=S.count)
32+
h = len(a) // 2
33+
a[1::2], a[::2] = a[:h], a[h:]
34+
return ''.join(a) if (a[-1:] != a[-2:-1]) else "" # same as >> return ''.join(a) * (a[-1:] != a[-2:-1])
35+
36+
37+
38+
39+
40+
41+
sol = Solution()
42+
S = "aaab"
43+
out = sol.reorganizeString(S)
44+
print("Res: ", out)

0 commit comments

Comments
 (0)