Advertisement
1988coder

358. Rearrange String k Distance Apart

Nov 21st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. /*
  2. Time Complexity: O(N logN)
  3. Space Complexity: O(N)
  4. N = length of the string.
  5. */
  6. class Solution {
  7.     public String rearrangeString(String s, int k) {
  8.         if (s == null || s.length() == 0) {
  9.             return "";
  10.         }
  11.         if (s.length() == 1 || k == 0) {
  12.             return s;
  13.         }
  14.        
  15.         Map<Character, Integer> map = new HashMap();
  16.         for (char c : s.toCharArray()) {
  17.             map.put(c, map.getOrDefault(c, 0) + 1);
  18.         }
  19.        
  20.         PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<Map.Entry<Character, Integer>>(
  21.             (a,b) -> (a.getValue() == b.getValue() ? a.getKey() - b.getKey() : b.getValue() - a.getValue()));
  22.         queue.addAll(map.entrySet());
  23.        
  24.         StringBuilder sb = new StringBuilder();
  25.        
  26.         while (!queue.isEmpty()) {
  27.             int n = k;
  28.             List<Map.Entry<Character, Integer>> waitingList = new LinkedList();
  29.             while (n > 0 && !queue.isEmpty()) {
  30.                 Map.Entry<Character, Integer> topEntry = queue.poll();
  31.                 sb.append(topEntry.getKey());
  32.                 topEntry.setValue(topEntry.getValue() - 1);
  33.                 waitingList.add(topEntry);
  34.                 n--;
  35.             }
  36.             for (Map.Entry<Character, Integer> entry : waitingList) {
  37.                 if (entry.getValue() > 0) {
  38.                     queue.offer(entry);
  39.                 }
  40.             }
  41.             if (n > 0 && !queue.isEmpty()) {
  42.                 return "";
  43.             }
  44.             waitingList.clear();
  45.         }
  46.         return sb.toString();
  47.     }
  48. }
  49.  
  50.  
  51. /*
  52. Above solution does not worn in leetcode. Thus replacing the ArrayList with a queue.
  53.  
  54. Time Complexity: O(N logN)
  55. Space Complexity: O(N)
  56. N = length of the string.
  57. */
  58. class Solution {
  59.     public String rearrangeString(String s, int k) {
  60.         if (s == null || s.length() == 0) {
  61.             return "";
  62.         }
  63.         if (s.length() == 1 || k == 0) {
  64.             return s;
  65.         }
  66.        
  67.         Map<Character, Integer> map = new HashMap();
  68.         for (char c : s.toCharArray()) {
  69.             map.put(c, map.getOrDefault(c, 0) + 1);
  70.         }
  71.        
  72.         PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<Map.Entry<Character, Integer>>(
  73.             (a,b) -> (a.getValue() == b.getValue() ? a.getKey() - b.getKey() : b.getValue() - a.getValue()));
  74.         queue.addAll(map.entrySet());
  75.        
  76.         StringBuilder sb = new StringBuilder();
  77.         Queue<Map.Entry<Character, Integer>> waitingList = new LinkedList();
  78.  
  79.         while (!queue.isEmpty()) {
  80.             Map.Entry<Character, Integer> topEntry = queue.poll();
  81.             sb.append(topEntry.getKey());
  82.             topEntry.setValue(topEntry.getValue() - 1);
  83.             waitingList.offer(topEntry);
  84.            
  85.             if (waitingList.size() < k) {
  86.                 continue;
  87.             }
  88.            
  89.             Map.Entry<Character, Integer> front = waitingList.poll();
  90.             if (front.getValue() > 0) {
  91.                 queue.offer(front);
  92.             }
  93.         }
  94.         return sb.length() == s.length() ? sb.toString() : "";
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement