Skip to content

Commit 1300483

Browse files
committed
feat: Reorganize String
1 parent 861f7bc commit 1300483

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

JavaScript/0767. Reorganize String.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
2+
// If possible, output any possible result. If not possible, return the empty string.
3+
//
4+
// Example 1:
5+
//
6+
// Input: S = "aab"
7+
// Output: "aba"
8+
//
9+
// Example 2:
10+
//
11+
// Input: S = "aaab"
12+
// Output: ""
13+
//
14+
// Note:
15+
//
16+
// S will consist of lowercase letters and have length in range [1, 500].
17+
18+
/**
19+
* @param {string} S
20+
* @return {string}
21+
*/
22+
23+
/** Priority queue */
24+
// JavaScript is lack of priority queue, check Python version

Python/0767. Reorganize String.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
2+
# If possible, output any possible result. If not possible, return the empty string.
3+
#
4+
# Example 1:
5+
#
6+
# Input: S = "aab"
7+
# Output: "aba"
8+
#
9+
# Example 2:
10+
#
11+
# Input: S = "aaab"
12+
# Output: ""
13+
#
14+
# Note:
15+
#
16+
# S will consist of lowercase letters and have length in range [1, 500].
17+
18+
19+
"""
20+
Priority queue
21+
"""
22+
# So at max we need the top 2 highest counts.
23+
# so pre_count, pre_c is to store the previous count and c which is (count and value)
24+
#
25+
# e.g. "aab"
26+
# If you take the counts and put that in heap it becomes [(-2, 'a'), (-1, 'b')]
27+
# Now you need a way to alternate the top 2 picks (just so their adj characters are different)
28+
# You already know pre_count and pre_c is to store the prev counts, so initially they are 0 and "" (0 counts and no string)
29+
#
30+
# 1st iteration:
31+
#
32+
# 1. count, c = heapq.heappop(pq) -> Taking out the top value which is (-2, 'a')
33+
# 2. res += c => add that to result string; result string becomes "a"
34+
# 3. count += 1 => since we took 1 a out of 2 a we increment the count so now the heap value of a becomes (-1, 'a')
35+
# 4. pre_count, pre_c = count, c => capture the a and updated count 1 to pre_count and pre_c
36+
#
37+
# 2nd iteration:
38+
# Now the heap only has (-1, 'b') (why? heappoped (-2, a) in 1st iteration
39+
# Now perform 1, 2 from 1st iteration => result string becomes "ab"
40+
# Now check your prev pre_count and pre_c since there is a remaining count of -1 for pre_count which means the prev top element is not exhausted yet, so push it back to heap heapq.heappush(pq, (pre_count, pre_c))
41+
#
42+
# 3 iteration:
43+
# Same as 1st iteration.
44+
#
45+
# Since you dont have any values in the heap you check if the res string is == S, why ? (if your input is like 'aaa' then the heap approach will give you 'aaa' which is same as S)
46+
47+
import heapq
48+
from collections import Counter
49+
50+
51+
class Solution:
52+
def reorganizeString(self, S: str) -> str:
53+
res = ""
54+
q = []
55+
counter = Counter(S)
56+
for c, count in counter.items():
57+
heapq.heappush(q, (-count, c))
58+
pre_count, pre_c = 0, ""
59+
while q:
60+
count, c = heapq.heappop(q)
61+
res += c
62+
if pre_count < 0:
63+
heapq.heappush(q, (pre_count, pre_c))
64+
count += 1
65+
pre_count, pre_c = count, c
66+
67+
if len(res) != len(S): return ""
68+
return res

0 commit comments

Comments
 (0)