|
| 1 | +package LeetCodeJava.Greedy; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/reorganize-string/description/ |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | + |
| 8 | +/** |
| 9 | + * 767. Reorganize String |
| 10 | + * Solved |
| 11 | + * Medium |
| 12 | + * Topics |
| 13 | + * Companies |
| 14 | + * Hint |
| 15 | + * Given a string s, rearrange the characters of s so that any two adjacent characters are not the same. |
| 16 | + * |
| 17 | + * Return any possible rearrangement of s or return "" if not possible. |
| 18 | + * |
| 19 | + * |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * |
| 23 | + * Input: s = "aab" |
| 24 | + * Output: "aba" |
| 25 | + * Example 2: |
| 26 | + * |
| 27 | + * Input: s = "aaab" |
| 28 | + * Output: "" |
| 29 | + * |
| 30 | + * |
| 31 | + * Constraints: |
| 32 | + * |
| 33 | + * 1 <= s.length <= 500 |
| 34 | + * s consists of lowercase English letters. |
| 35 | + * |
| 36 | + * |
| 37 | + */ |
| 38 | +public class ReorganizeString { |
| 39 | + |
| 40 | + // V0 |
| 41 | + // IDEA : HASHMAP |
| 42 | + // TODO : fix below |
| 43 | +// public String reorganizeString(String s) { |
| 44 | +// |
| 45 | +// if (s.length() == 1){ |
| 46 | +// return s; |
| 47 | +// } |
| 48 | +// |
| 49 | +// ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>(); |
| 50 | +// for (String x : s.split("")){ |
| 51 | +// map.put(x, map.getOrDefault(x, 0)+1); |
| 52 | +// } |
| 53 | +// |
| 54 | +// System.out.println(">>> map = " + map); |
| 55 | +// StringBuilder sb = new StringBuilder(); |
| 56 | +// String prev = null; |
| 57 | +// //String res = ""; |
| 58 | +// |
| 59 | +// while(!map.isEmpty()){ |
| 60 | +// for(String k : map.keySet()){ |
| 61 | +// System.out.println(">>> k = " + k + ", keySet = " + map.keySet() + ", prev = " + prev); |
| 62 | +// if (prev != null && prev.equals(k)){ |
| 63 | +// return ""; |
| 64 | +// } |
| 65 | +// sb.append(k); |
| 66 | +// prev = k; |
| 67 | +// if (map.get(k) - 1 == 0){ |
| 68 | +// map.remove(k); |
| 69 | +// }else{ |
| 70 | +// map.put(k, map.get(k)-1); |
| 71 | +// } |
| 72 | +// } |
| 73 | +// } |
| 74 | +// |
| 75 | +// return sb.toString(); |
| 76 | +// } |
| 77 | + |
| 78 | + // V1 |
| 79 | + // IDEA : HASHMAP + PriorityQueue |
| 80 | + // https://leetcode.com/problems/reorganize-string/solutions/943268/heap-fetch-2-at-once-with-very-detail-explanations/ |
| 81 | + public String reorganizeString_1(String S) { |
| 82 | + |
| 83 | + // step 1: |
| 84 | + // build a hashmap to store characters and its frequencies: |
| 85 | + Map<Character, Integer> freq_map = new HashMap<>(); |
| 86 | + for (char c : S.toCharArray()) { |
| 87 | + freq_map.put(c, freq_map.getOrDefault(c, 0) + 1); |
| 88 | + } |
| 89 | + // step 2: |
| 90 | + // put the char of freq_map into the maxheap with sorting the frequencies by |
| 91 | + // large->small |
| 92 | + /** NOTE !!!! |
| 93 | + * |
| 94 | + * make PQ as descending order |
| 95 | + * |
| 96 | + * (a, b) -> freq_map.get(b) - freq_map.get(a) |
| 97 | + */ |
| 98 | + PriorityQueue<Character> maxheap = new PriorityQueue<>( |
| 99 | + (a, b) -> freq_map.get(b) - freq_map.get(a) |
| 100 | + ); |
| 101 | + // addAll() is adding more then one element to heap |
| 102 | + maxheap.addAll(freq_map.keySet()); |
| 103 | + |
| 104 | + // now maxheap has the most frequent character on the top |
| 105 | + |
| 106 | + // step 3: |
| 107 | + // obtain the character 2 by 2 from the maxheap to put in the result sb |
| 108 | + // until there is only one element(character) left in the maxheap |
| 109 | + // create a stringbuilder to build the result result |
| 110 | + StringBuilder sb = new StringBuilder(); |
| 111 | + while (maxheap.size() > 1) { |
| 112 | + char first = maxheap.poll(); |
| 113 | + char second = maxheap.poll(); |
| 114 | + sb.append(first); |
| 115 | + sb.append(second); |
| 116 | + freq_map.put(first, freq_map.get(first) - 1); |
| 117 | + freq_map.put(second, freq_map.get(second) - 1); |
| 118 | + |
| 119 | + // insert the character back to the freq_map if the count in |
| 120 | + // hashmap of these two character are still > 0 |
| 121 | + if (freq_map.get(first) > 0) { |
| 122 | + maxheap.offer(first); |
| 123 | + } |
| 124 | + if (freq_map.get(second) > 0) { |
| 125 | + maxheap.offer(second); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (!maxheap.isEmpty()) { |
| 130 | + // when there is only 1 element left in the maxheap |
| 131 | + // check the count, it should not be greater than 1 |
| 132 | + // otherwise it would be impossible and should return "" |
| 133 | + if (freq_map.get(maxheap.peek()) > 1) { |
| 134 | + return ""; |
| 135 | + } else { |
| 136 | + sb.append(maxheap.poll()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return sb.toString(); |
| 141 | + } |
| 142 | + |
| 143 | + // V2 |
| 144 | + // https://leetcode.com/problems/reorganize-string/solutions/3948228/100-fast-priorityqueue-with-explanation-c-java-python-c/ |
| 145 | + public String reorganizeString_2(String s) { |
| 146 | + int[] f = new int[26]; |
| 147 | + int n = s.length(); |
| 148 | + |
| 149 | + for (int i = 0; i < n; i++) { |
| 150 | + f[s.charAt(i) - 'a']++; |
| 151 | + if (f[s.charAt(i) - 'a'] > (n + 1) / 2) { |
| 152 | + return ""; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + PriorityQueue<Pair> p = new PriorityQueue<>((a, b) -> b.freq - a.freq); |
| 157 | + for (int i = 0; i < 26; i++) { |
| 158 | + if (f[i] != 0) { |
| 159 | + p.offer(new Pair(f[i], (char) (i + 'a'))); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + StringBuilder ans = new StringBuilder(); |
| 164 | + while (p.size() >= 2) { |
| 165 | + Pair p1 = p.poll(); |
| 166 | + Pair p2 = p.poll(); |
| 167 | + ans.append(p1.ch); |
| 168 | + ans.append(p2.ch); |
| 169 | + if (p1.freq > 1) { |
| 170 | + p.offer(new Pair(p1.freq - 1, p1.ch)); |
| 171 | + } |
| 172 | + if (p2.freq > 1) { |
| 173 | + p.offer(new Pair(p2.freq - 1, p2.ch)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (!p.isEmpty()) { |
| 178 | + ans.append(p.poll().ch); |
| 179 | + } |
| 180 | + |
| 181 | + return ans.toString(); |
| 182 | + } |
| 183 | + |
| 184 | + class Pair { |
| 185 | + int freq; |
| 186 | + char ch; |
| 187 | + |
| 188 | + Pair(int freq, char ch) { |
| 189 | + this.freq = freq; |
| 190 | + this.ch = ch; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + // V3 |
| 196 | + // https://leetcode.com/problems/reorganize-string/solutions/3948110/easy-solution-python3-c-c-java-python-with-image/ |
| 197 | + public String reorganizeString_3(String s) { |
| 198 | + Map<Character, Integer> count = new HashMap<>(); |
| 199 | + for (char c : s.toCharArray()) { |
| 200 | + count.put(c, count.getOrDefault(c, 0) + 1); |
| 201 | + } |
| 202 | + |
| 203 | + List<int[]> maxHeap = new ArrayList<>(); |
| 204 | + for (Map.Entry<Character, Integer> entry : count.entrySet()) { |
| 205 | + maxHeap.add(new int[]{-entry.getValue(), entry.getKey()}); |
| 206 | + } |
| 207 | + heapify(maxHeap); |
| 208 | + |
| 209 | + int[] prev = null; |
| 210 | + StringBuilder res = new StringBuilder(); |
| 211 | + while (!maxHeap.isEmpty() || prev != null) { |
| 212 | + if (prev != null && maxHeap.isEmpty()) { |
| 213 | + return ""; |
| 214 | + } |
| 215 | + |
| 216 | + int[] top = heapPop(maxHeap); |
| 217 | + res.append((char) top[1]); |
| 218 | + top[0]++; |
| 219 | + |
| 220 | + if (prev != null) { |
| 221 | + heapPush(maxHeap, prev); |
| 222 | + prev = null; |
| 223 | + } |
| 224 | + |
| 225 | + if (top[0] != 0) { |
| 226 | + prev = top; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + return res.toString(); |
| 231 | + } |
| 232 | + |
| 233 | + private void heapify(List<int[]> heap) { |
| 234 | + int n = heap.size(); |
| 235 | + for (int i = n / 2 - 1; i >= 0; i--) { |
| 236 | + heapifyDown(heap, i); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + private void heapifyDown(List<int[]> heap, int index) { |
| 241 | + int n = heap.size(); |
| 242 | + int left = 2 * index + 1; |
| 243 | + int right = 2 * index + 2; |
| 244 | + int largest = index; |
| 245 | + |
| 246 | + if (left < n && heap.get(left)[0] < heap.get(largest)[0]) { |
| 247 | + largest = left; |
| 248 | + } |
| 249 | + if (right < n && heap.get(right)[0] < heap.get(largest)[0]) { |
| 250 | + largest = right; |
| 251 | + } |
| 252 | + |
| 253 | + if (largest != index) { |
| 254 | + Collections.swap(heap, index, largest); |
| 255 | + heapifyDown(heap, largest); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + private int[] heapPop(List<int[]> heap) { |
| 260 | + int n = heap.size(); |
| 261 | + int[] top = heap.get(0); |
| 262 | + heap.set(0, heap.get(n - 1)); |
| 263 | + heap.remove(n - 1); |
| 264 | + heapifyDown(heap, 0); |
| 265 | + return top; |
| 266 | + } |
| 267 | + |
| 268 | + private void heapPush(List<int[]> heap, int[] element) { |
| 269 | + heap.add(element); |
| 270 | + heapifyUp(heap, heap.size() - 1); |
| 271 | + } |
| 272 | + |
| 273 | + private void heapifyUp(List<int[]> heap, int index) { |
| 274 | + while (index > 0) { |
| 275 | + int parent = (index - 1) / 2; |
| 276 | + if (heap.get(index)[0] >= heap.get(parent)[0]) { |
| 277 | + break; |
| 278 | + } |
| 279 | + Collections.swap(heap, index, parent); |
| 280 | + index = parent; |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + // V3 |
| 285 | +} |
0 commit comments