-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorganizeString.java
More file actions
48 lines (37 loc) · 1.61 KB
/
ReorganizeString.java
File metadata and controls
48 lines (37 loc) · 1.61 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
import java.util.*;
class ReorganizeString{
static String reorganize(String s){
Map<Character, Integer> map= new HashMap<>();
//Build a map with freq
for(char c: s.toCharArray())
map.put(c, map.getOrDefault(c, 0) + 1);
//build a max heap
PriorityQueue<Character> pq= new PriorityQueue<>((a, b) -> map.get(b) - map.get(a));
pq.addAll(map.keySet());
StringBuilder sb= new StringBuilder();
while(pq.size() > 1){
char curr= pq.remove(); //remove the 1st char with most freq
char next= pq.remove(); //remove the 2nd higehst freq char
sb.append(curr); //Append both to res
sb.append(next);
map.put(curr, map.get(curr) - 1); //decreas their count in map
map.put(next, map.get(next) - 1);
if(map.get(curr) > 0) pq.add(curr); //if their freq > 0 add them to pq again
if(map.get(next) > 0) pq.add(next);
}
if(!pq.isEmpty()){
//Take the last char
char curr= pq.remove();
//If the char freq freq is more than one then its not possible to build string
if(map.get(curr) > 1) return "";
else sb.append(curr); //else build the string and return
}
return sb.toString();
}
public static void main(String[] args) {
String s="aab";
//Reorganize in such a manner such that no two adjacent string repeat
// o/p aba
System.out.println(reorganize(s));
}
}