-
Notifications
You must be signed in to change notification settings - Fork 43
/
ReorganizeString.java
408 lines (356 loc) · 12.3 KB
/
ReorganizeString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package LeetCodeJava.Greedy;
// https://leetcode.com/problems/reorganize-string/description/
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 767. Reorganize String
* Solved
* Medium
* Topics
* Companies
* Hint
* Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.
*
* Return any possible rearrangement of s or return "" if not possible.
*
*
*
* Example 1:
*
* Input: s = "aab"
* Output: "aba"
* Example 2:
*
* Input: s = "aaab"
* Output: ""
*
*
* Constraints:
*
* 1 <= s.length <= 500
* s consists of lowercase English letters.
*
*
*/
public class ReorganizeString {
// V0
// IDEA : HASHMAP + HEAP
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Greedy/reorganize-string.py#L35
public String reorganizeString(String S) {
// Step 1: Count the frequency of each character
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : S.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
// Step 2: Use a priority queue (max heap) to keep characters sorted by
// frequency
/** NOTE !!!
*
* we use PQ to track the characters count sorted in order
*/
PriorityQueue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>(
(a, b) -> b.getValue() - a.getValue());
maxHeap.addAll(charCountMap.entrySet());
// Step 3: Initialize a StringBuilder with a placeholder to avoid indexing
// errors
StringBuilder result = new StringBuilder("#");
// Step 4: While the heap is not empty, try to append characters
while (!maxHeap.isEmpty()) {
boolean stop = true;
// Iterate over the heap to find the most common character that isn't the last
// character in the result
List<Map.Entry<Character, Integer>> tempList = new ArrayList<>();
while (!maxHeap.isEmpty()) {
Map.Entry<Character, Integer> entry = maxHeap.poll();
char currentChar = entry.getKey();
int count = entry.getValue();
// If the current character is not the same as the last character in the result
/**
* NOTE !!!
*
* we get last element of stringBuilder via
*
* sb.charAt(sb.length() - 1)
*/
if (currentChar != result.charAt(result.length() - 1)) {
stop = false;
result.append(currentChar);
// Decrease the count and add it back to the heap if it's still > 0
if (count - 1 > 0) {
entry.setValue(count - 1);
tempList.add(entry);
}
break;
} else {
tempList.add(entry);
}
}
// Add back all remaining entries to the heap
maxHeap.addAll(tempList);
// If no valid character was found, break the loop
if (stop) {
break;
}
}
// Step 5: Return the result or an empty string if reorganization is not
// possible
String reorganizedString = result.substring(1); // Remove the placeholder
return reorganizedString.length() == S.length() ? reorganizedString : "";
}
// V0'
// IDEA : HASHMAP
// TODO : fix below
// public String reorganizeString(String s) {
//
// if (s.length() == 1){
// return s;
// }
//
// ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
// for (String x : s.split("")){
// map.put(x, map.getOrDefault(x, 0)+1);
// }
//
// System.out.println(">>> map = " + map);
// StringBuilder sb = new StringBuilder();
// String prev = null;
// //String res = "";
//
// while(!map.isEmpty()){
// for(String k : map.keySet()){
// System.out.println(">>> k = " + k + ", keySet = " + map.keySet() + ", prev = " + prev);
// if (prev != null && prev.equals(k)){
// return "";
// }
// sb.append(k);
// prev = k;
// if (map.get(k) - 1 == 0){
// map.remove(k);
// }else{
// map.put(k, map.get(k)-1);
// }
// }
// }
//
// return sb.toString();
// }
// V1
// IDEA : HASHMAP + PriorityQueue
// https://leetcode.com/problems/reorganize-string/solutions/943268/heap-fetch-2-at-once-with-very-detail-explanations/
public String reorganizeString_1(String S) {
// step 1:
// build a hashmap to store characters and its frequencies:
Map<Character, Integer> freq_map = new HashMap<>();
for (char c : S.toCharArray()) {
freq_map.put(c, freq_map.getOrDefault(c, 0) + 1);
}
// step 2:
// put the char of freq_map into the maxheap with sorting the frequencies by
// large->small
/** NOTE !!!!
*
* make PQ as descending order
*
* (a, b) -> freq_map.get(b) - freq_map.get(a)
*/
PriorityQueue<Character> maxheap = new PriorityQueue<>(
(a, b) -> freq_map.get(b) - freq_map.get(a)
);
// addAll() is adding more then one element to heap
maxheap.addAll(freq_map.keySet());
// now maxheap has the most frequent character on the top
// step 3:
// obtain the character 2 by 2 from the maxheap to put in the result sb
// until there is only one element(character) left in the maxheap
// create a stringbuilder to build the result result
StringBuilder sb = new StringBuilder();
while (maxheap.size() > 1) {
char first = maxheap.poll();
char second = maxheap.poll();
sb.append(first);
sb.append(second);
freq_map.put(first, freq_map.get(first) - 1);
freq_map.put(second, freq_map.get(second) - 1);
// insert the character back to the freq_map if the count in
// hashmap of these two character are still > 0
if (freq_map.get(first) > 0) {
maxheap.offer(first);
}
if (freq_map.get(second) > 0) {
maxheap.offer(second);
}
}
if (!maxheap.isEmpty()) {
// when there is only 1 element left in the maxheap
// check the count, it should not be greater than 1
// otherwise it would be impossible and should return ""
if (freq_map.get(maxheap.peek()) > 1) {
return "";
} else {
sb.append(maxheap.poll());
}
}
return sb.toString();
}
// V2
// IDEA : HASHMAP + array + 26 alphabet consideration (gpt)
public String reorganizeString_2(String S) {
// Step 1: Count the frequency of each character using an array
int[] charCounts = new int[26];
for (char c : S.toCharArray()) {
charCounts[c - 'a']++;
}
// Step 2: Find the most frequent character
int maxCount = 0, maxCharIndex = -1;
for (int i = 0; i < 26; i++) {
if (charCounts[i] > maxCount) {
maxCount = charCounts[i];
maxCharIndex = i;
}
}
// Step 3: If the most frequent character is more than half the string length, return ""
if (maxCount > (S.length() + 1) / 2) {
return "";
}
// Step 4: Arrange characters
char[] result = new char[S.length()];
int index = 0;
// Fill the most frequent character first
while (charCounts[maxCharIndex] > 0) {
result[index] = (char) (maxCharIndex + 'a');
index += 2; // Fill at even indices first
charCounts[maxCharIndex]--;
}
// Fill the remaining characters
for (int i = 0; i < 26; i++) {
while (charCounts[i] > 0) {
if (index >= S.length()) {
index = 1; // Switch to odd indices
}
result[index] = (char) (i + 'a');
index += 2;
charCounts[i]--;
}
}
return new String(result);
}
// V3
// https://leetcode.com/problems/reorganize-string/solutions/3948228/100-fast-priorityqueue-with-explanation-c-java-python-c/
public String reorganizeString_3(String s) {
int[] f = new int[26];
int n = s.length();
for (int i = 0; i < n; i++) {
f[s.charAt(i) - 'a']++;
if (f[s.charAt(i) - 'a'] > (n + 1) / 2) {
return "";
}
}
PriorityQueue<Pair> p = new PriorityQueue<>((a, b) -> b.freq - a.freq);
for (int i = 0; i < 26; i++) {
if (f[i] != 0) {
p.offer(new Pair(f[i], (char) (i + 'a')));
}
}
StringBuilder ans = new StringBuilder();
while (p.size() >= 2) {
Pair p1 = p.poll();
Pair p2 = p.poll();
ans.append(p1.ch);
ans.append(p2.ch);
if (p1.freq > 1) {
p.offer(new Pair(p1.freq - 1, p1.ch));
}
if (p2.freq > 1) {
p.offer(new Pair(p2.freq - 1, p2.ch));
}
}
if (!p.isEmpty()) {
ans.append(p.poll().ch);
}
return ans.toString();
}
class Pair {
int freq;
char ch;
Pair(int freq, char ch) {
this.freq = freq;
this.ch = ch;
}
}
// V4
// https://leetcode.com/problems/reorganize-string/solutions/3948110/easy-solution-python3-c-c-java-python-with-image/
public String reorganizeString_4(String s) {
Map<Character, Integer> count = new HashMap<>();
for (char c : s.toCharArray()) {
count.put(c, count.getOrDefault(c, 0) + 1);
}
List<int[]> maxHeap = new ArrayList<>();
for (Map.Entry<Character, Integer> entry : count.entrySet()) {
maxHeap.add(new int[]{-entry.getValue(), entry.getKey()});
}
heapify(maxHeap);
int[] prev = null;
StringBuilder res = new StringBuilder();
while (!maxHeap.isEmpty() || prev != null) {
if (prev != null && maxHeap.isEmpty()) {
return "";
}
int[] top = heapPop(maxHeap);
res.append((char) top[1]);
top[0]++;
if (prev != null) {
heapPush(maxHeap, prev);
prev = null;
}
if (top[0] != 0) {
prev = top;
}
}
return res.toString();
}
private void heapify(List<int[]> heap) {
int n = heap.size();
for (int i = n / 2 - 1; i >= 0; i--) {
heapifyDown(heap, i);
}
}
private void heapifyDown(List<int[]> heap, int index) {
int n = heap.size();
int left = 2 * index + 1;
int right = 2 * index + 2;
int largest = index;
if (left < n && heap.get(left)[0] < heap.get(largest)[0]) {
largest = left;
}
if (right < n && heap.get(right)[0] < heap.get(largest)[0]) {
largest = right;
}
if (largest != index) {
Collections.swap(heap, index, largest);
heapifyDown(heap, largest);
}
}
private int[] heapPop(List<int[]> heap) {
int n = heap.size();
int[] top = heap.get(0);
heap.set(0, heap.get(n - 1));
heap.remove(n - 1);
heapifyDown(heap, 0);
return top;
}
private void heapPush(List<int[]> heap, int[] element) {
heap.add(element);
heapifyUp(heap, heap.size() - 1);
}
private void heapifyUp(List<int[]> heap, int index) {
while (index > 0) {
int parent = (index - 1) / 2;
if (heap.get(index)[0] >= heap.get(parent)[0]) {
break;
}
Collections.swap(heap, index, parent);
index = parent;
}
}
// V5
}