-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_34.java
More file actions
65 lines (52 loc) · 2.26 KB
/
Problem_34.java
File metadata and controls
65 lines (52 loc) · 2.26 KB
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
package strings;
import java.util.HashMap;
import java.util.PriorityQueue;
public class Problem_34 {
public static String rearrangeString(String str) {
if (str == null || str.isEmpty()) {
return str; // Empty string has no characters to rearrange
}
// Count character frequencies
HashMap<Character, Integer> charCount = new HashMap<>();
for (char c : str.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
// Create a priority queue to store characters based on their frequencies
// (highest frequency first)
PriorityQueue<Character> pq = new PriorityQueue<>((a, b) -> charCount.get(b) - charCount.get(a));
pq.addAll(charCount.keySet());
// Characters to be used in the rearranged string
StringBuilder result = new StringBuilder();
// Previous character used (initialize with a special character not present in
// the string)
char prev = '#';
while (!pq.isEmpty()) {
char curr = pq.poll();
// Check if the previous character is the same, skip if so (to avoid adjacent
// duplicates)
if (curr == prev) {
if (pq.isEmpty()) {
return ""; // Not possible to rearrange without adjacent duplicates
}
char temp = pq.poll(); // Get the next character and put the current one back for later processing
pq.offer(curr);
curr = temp;
}
// Add the current character to the result and update the previous character
result.append(curr);
charCount.put(curr, charCount.get(curr) - 1); // Decrement frequency
// If the frequency of the current character is not zero (can be used again
// later), add it back to the queue
if (charCount.get(curr) > 0) {
pq.offer(curr);
}
prev = curr;
}
return result.toString();
}
public static void main(String[] args) {
String str = "aab";
String rearrangedString = rearrangeString(str);
System.out.println("Rearranged string without adjacent duplicates: " + rearrangedString);
}
}