-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorganizeString.java
More file actions
86 lines (73 loc) · 2.21 KB
/
ReorganizeString.java
File metadata and controls
86 lines (73 loc) · 2.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package Heap;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
/**
* @author Vishal Singh
* 7/25/2021
* @link https://leetcode.com/problems/reorganize-string/
*/
/*
* 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: ""
* */
public class ReorganizeString {
class Pair implements Comparable<Pair> {
char ch;
int freq;
public Pair(char ch, int freq) {
this.ch = ch;
this.freq = freq;
}
@Override
public int compareTo(Pair pair) {
return this.freq - pair.freq;
}
}
class Solution {
public String reorganizeString(String s) {
char[] arr = s.toCharArray();
HashMap<Character, Integer> freq = new HashMap<>();
for (char ch : arr)
freq.put(ch, freq.getOrDefault(ch, 0) + 1);
PriorityQueue<Pair> pq = new PriorityQueue<>(Collections.reverseOrder());
for (char key : freq.keySet()) {
pq.add(new Pair(key, freq.get(key)));
}
StringBuilder sb = new StringBuilder();
Pair wait = null;
while (!pq.isEmpty() || wait != null) {
if (pq.isEmpty()) {
pq.add(wait);
wait = null;
}
Pair p = pq.poll();
sb.append(p.ch);
if (wait != null) {
pq.add(wait);
}
if (p.freq == 1) {
wait = null;
} else {
wait = new Pair(p.ch, p.freq - 1);
}
}
boolean poss = true;
for (int i = 0; i < sb.length() - 1; i++) {
if (sb.charAt(i) == sb.charAt(i + 1)) {
poss = false;
break;
}
}
if (!poss) return "";
return sb.toString();
}
}
}