Given a string and a positive integer d. Some characters may be repeated in the given string. Rearrange characters of the given string such that the same characters become d distance away from each other. Note that there can be many possible rearrangements, the output should be one of the possible rearrangements. If no such arrangement is possible, that should also be reported.
Expected time complexity is O(n) where n is length of input string.
Solution: The idea is to count frequencies of all characters and consider the most frequent character first and place all occurrences of it as close as possible. After the most frequent character is placed, repeat the same process for remaining characters.
1) Let the given string be str and size of string be n
2) Traverse str, store all characters and their frequencies in a Max Heap MH. The value of frequency decides the order in MH, i.e., the most frequent character is at the root of MH.
3) Make all characters of str as ‘\0′.
4) Do following while MH is not empty.
…a) Extract the Most frequent character. Let the extracted character be x and its frequency be f.
…b) Find the first available position in str, i.e., find the first ‘\0′ in str.
…c) Let the first position be p. Fill x at p, p+d,.. p+(f-1)d
Examples: Input: "abb", d = 2 Output: "bab" Input: "aacbbc", d = 3 Output: "abcabc" Input: "geeksforgeeks", d = 3 Output: egkegkesfesor Input: "aaa", d = 2 Output: Cannot be rearranged
public static String rearrange(String str, int k) { Map<Character, Integer> map = new HashMap<>(); char[] s = str.toCharArray(); int n = s.length; for(int i=0; i<n; i++) { Integer cnt = map.get(s[i]); if(cnt == null) cnt = 0; map.put(s[i], cnt+1); } Queue<Character> queue = new PriorityQueue<>(map.size(), new Comparator<Character>(){ public int compare(Character c1, Character c2) { return map.get(c2) - map.get(c1); } }); queue.addAll(map.keySet()); Arrays.fill(s, '\0'); for(int i=0; i<map.size(); i++) { int p = i; while(s[p] != '\0') p++; char c = queue.poll(); int cnt = map.get(c); for(int j=0; j<cnt; j++) { if(p >= n) return "Cannot be rearranged"; s[p] = c; p += k; } } return new String(s); }
Time Complexity: O(n+mlog(m)). Here n is the length of str, m is count of distinct characters in str[]. m is smaller than 256. So the time complexity can be considered as O(n).
Reference:
相关推荐
使用键盘快捷键轻松快速地重新排列选项卡。 QuickShift Redux是一个Chrome扩展程序,可让您轻松快捷地使用键盘快捷键重新排列标签页。 -在同一窗口内向左或向右移动选项卡-在新窗口中将选项卡取消停靠-在窗口之间...
Finally, a ground-up, quick-start approach to XSLT that teaches not just the language, but XML processing solutions. XSLT Jumpstarter approaches the subject like no other book, using examples that ...
Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) repository. I'll keep updating for full summary and...
重新布置房间 网站总览 房间改造共享网站 网站主题 丰富回国时间 选择主题的原因 当前,出门旅行的限制比以前更多,例如远程工作。在这种情况下,我感到越来越多的人正在他们的家中寻找舒适的环境和空间。...
**URL Rearrange-crx插件详解** URL Rearrange-crx是一款专为浏览器设计的扩展程序,主要用于帮助用户根据个人需求对URL(统一资源定位符)进行拆分和合并操作。这款插件尤其适用于处理长而复杂的网址,让用户能更...
Abstract This thesis presents contributions to the state-of-the-art in quadrotor control, payload transportation with ...quickly rearrange formations and avoid a small number of obstacles.
∷相关函数:Rearrange函数 1.5.6 希尔排序 241 范例1-82 希尔排序 241 ∷相关函数:ShellSort函数 1.5.7 冒泡排序 243 范例1-83 冒泡排序 243 ∷相关函数:bubble_sort函数 1.5.8 一趟快速排序 246 范例1-...
重新排列标签是一个小巧的谷歌浏览器扩展,允许用户 通过移动它们来重新排列窗口中标签的位置 围绕使用键盘快捷键。 将标签左移 Mac:Ctrl + Shift +向左 Windows:Shift + Alt +向左 向右移动标签 Mac:Ctrl + ...
and line control (if you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler about where each source ...
功能: 1. 立刻判断是否有解 2. 对于所有有解的情况,只要对搜索的状态数的限 制适当放宽,总是能够找到解的 3. 支持随机初始状态和自给定初始状态 4. 支持游戏模式 呵呵 ... java rearrange.Rearrange fg
这种方法称为“分开-重排-合并”(Split-Rearrange-Merge)算法,也称为“嵌套”或“迭代”FFT。 在提供的文件列表中,我们可以看到多个以"fft4"开头的源代码文件,这些可能是实现不同版本的2D FFT算法的C和Fortran...
This means that a search can cross verse boundaries, giving a more natural search. It also supports regular expressions and phrases. Flexible layout BPBible lets you rearrange your layout, and ...
重新排列标签页是一个很小的Google Chrome扩展程序,允许用户通过使用键盘快捷键在窗口中四处移动来重新排列标签页中的位置。 向左移动选定的选项卡Mac:Ctrl + Shift +向左的窗口:Shift + Alt +向左移动向右的Mac...
New editing commands “Columns” – Cut, copy, delete or rearrange columns easily delimited text files (CSV, TSV) without a spreadsheet or complicated regular expressions. FTP / SFTP browsers now have...
语言:English (United States) 允许您使用键盘快捷键重新排列标签。 此扩展名使您可以使用键盘...如果您发现与此有关的任何问题或有改进的想法,请在此处告诉我:https://github.com/chriskim06/tab-rearrange/issues
MESSAGE Copies a feature so that it can be pasted either within the same part or to another. SYNONYMS duplicate, repeat HINT This command is available in history modeling mode. ACTIONS STANDARD ...
It can encrypt, decrypt, join, split, rearrange, create and stamp a PDF file. For more information visit http://www.sheelapps.com/. PDFTools is developed using iText, a free Java-PDF library. iText...
- “ECRS”可能是“消除-合并-重新排序-简化(Eliminate-Combine-Rearrange-Simplify)”原则的缩写,这是一种用于改进流程的方法论。 由于提供的内容断断续续,很难给出准确的知识点解释。但可以确定的是,这些...