字符串重新排列,让里面不能有相同字母在一起。比如aaabbb非法的,要让它变成ababab。给一种即可

Greedy:

FB面经Prepare task Schedule II很像,记录每个char出现次数,然后用最大堆,把剩下char里面出现次数多的优先Poll出来组建新的string

如果poll出来的char跟上一个相同,则用一个queue暂时存一下

我觉得时间复杂度:O(N) + O(KlogK) + O(NlogK) = O(NlogK) ,where K is the number of different character in the string

 package ReorderString;
import java.util.*; public class Solution {
class Element {
char val;
int appear;
public Element(char value) {
this.val = value;
this.appear = 1;
}
} public String reorder(String str) {
Element[] summary = new Element[26];
for (int i=0; i<str.length(); i++) {
char cur = str.charAt(i);
if (summary[(int)(cur-'a')] == null) {
summary[(int)(cur-'a')] = new Element(cur);
}
else {
summary[(int)(cur-'a')].appear++;
}
}
PriorityQueue<Element> queue = new PriorityQueue<Element>(11, new Comparator<Element>() {
public int compare(Element e1, Element e2) {
return e2.appear - e1.appear;
}
}); for (Element each : summary) {
if (each != null) {
queue.offer(each);
}
}
Queue<Element> store = new LinkedList<Element>();
StringBuffer res = new StringBuffer();
while (!queue.isEmpty() || !store.isEmpty()) {
if (!queue.isEmpty()) {
Element cur = queue.poll();
if (res.length()==0 || cur.val!=res.charAt(res.length()-1)) {
res.append(cur.val);
cur.appear--;
if (cur.appear > 0) store.offer(cur);
while (!store.isEmpty()) {
queue.offer(store.poll());
}
}
else { //cur.val equals last char in res
store.offer(cur);
}
}
else { //store is not empty but queue is empty
res = new StringBuffer();
res.append(-1);
return res.toString();
}
}
return res.toString();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
String res = sol.reorder("aaabbba");
System.out.println(res);
} }

G面经prepare: Reorder String to make duplicates not consecutive的更多相关文章

  1. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

  2. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  3. G面经Prepare: Valid Preorder traversal serialized String

    求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...

  4. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  5. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  6. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  7. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  8. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

  9. G面经prepare: Chucked Palindrome

    给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起 ...

随机推荐

  1. flink - accumulator

      读accumlator JobManager 在job finish的时候会汇总accumulator的值, newJobStatus match { case JobStatus.FINISHE ...

  2. fatal error C1854: 无法覆盖在创建对象文件.obj”的预编译头过程中形成的信息

    原因: 将stdafx.cpp 的预编译头属性  由 创建预编译头(/Yc) 改成了 使用预编译头(/Yu) 解决: 改回为 创建预编译头(/Yc) 参考文档 http://blog.csdn.net ...

  3. 前端 JSer 装逼手册

    阅读 8143收藏 2352016-7-18 SegmentFault 分享:吉祥物 @ SegmentFault 在装逼成本越来越高的 JS 圈,是时候充值一下了 -- 题记. 作者:kenberk ...

  4. 在大数据中,关于native包的编译步骤

    一.问题的由来: 二.解决问题的方法(所有的操作在root下完成): 1.前期需要的环境,下面的已经在伪分布式中配置好,不再重复 配置好jdk 配置好hadoop 2.上传还需要包 apache-ma ...

  5. hadoop 2.4 伪分布式模式

    1.core-site.xml 在<configuration></configuration>中插入 <property> <name>fs.defa ...

  6. 各个JSON技术的比较

    JSON技术的调研报告 一 .各个JSON技术的简介和优劣1.json-libjson-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,包括com ...

  7. JSONModel 遇见关键字为id或者description

    像id.description这样的,都是系统自带的,要解析它,得特殊处理一下.我用的是JSONModel { "contentList": [ { "id": ...

  8. JS-006-表格元素操作

    直接上菜咯... 以下为 HTML 表格源码: <html> <head> <meta http-equiv="Content-Type" conte ...

  9. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  10. php实现斐波那契数列以及由此引起的联想

    斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一 ...