G面经prepare: Sort String Based On Another
Given a sorting order string, sort the input string based on the given sorting order string. Ex sorting order string -> dfbcae
Input string -> abcdeeabc
output -> dbbccaaee
法一:Comparable
sample Input:
String order = "dfbcae";
String str = "akbcdeeabc";
Sample Output: dbbccaaeekk
要注意13行,indexOf()没有matched到是return -1, 要把它设为最大
package SortStringBasedOnAnother;
import java.util.*; public class Solution {
public class Sortable implements Comparable<Sortable> { private Character content;
private int order; public Sortable(char str, String sortingOrder) {
this.content = str;
this.order = sortingOrder.indexOf(str);
if (this.order == -1) this.order = Integer.MAX_VALUE;
} public int compareTo(Sortable another) {
if (this.order > another.order) {
return 1;
} else if (this.order == another.order) {
return 0;
} else {
return -1;
}
} public String toString() {
return String.valueOf(content);
} } public void sort(String order, String str) {
Sortable[] array = new Sortable[str.length()];
for (int i = 0; i < str.length(); i++) {
array[i] = new Sortable(str.charAt(i), order);
}
Collections.sort(Arrays.asList(array)); //Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
} } public static void main(String[] args) {
Solution sol = new Solution();
String order = "dfbcae";
String str = "abcdkkeeabc";
sol.sort(order, str);
}
}
方法二:(Better)counting sort
If taking extra mem in allowed. Take an int array with size same as "sorting order string" that will maintain a count. now iterate the input string & keep on incrementing the corresponding char count in this new array.
记得要用一个queue记录没有在order里面的str的元素
package SortStringBasedOnAnother;
import java.util.*; public class Solution2 {
public void sort(String order, String str) {
int[] count = new int[order.length()];
Queue<Character> notInOrder = new LinkedList<Character>();
StringBuffer sb = new StringBuffer();
for (int i=0; i<str.length(); i++) {
boolean matched = false;
char cur = str.charAt(i);
for (int j=0; j<order.length(); j++) {
if (cur == order.charAt(j)) {
count[j]++;
matched = true;
break;
}
}
if (!matched) notInOrder.offer(cur);
}
for (int i=0; i<count.length; i++) {
while (count[i] > 0) {
sb.append(order.charAt(i));
count[i]--;
}
}
while (!notInOrder.isEmpty()) {
sb.append(notInOrder.poll());
}
System.out.println(sb.toString());
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution2 sol = new Solution2();
String order = "dfbcae";
String str = "akbcdeeabc";
sol.sort(order, str); } }
12-19行可以用IndexOf替代
char cur = str.charAt(i);
int index = order.indexOf(cur);
if (index == -1) notInOrder.offer(cur);
else count[index]++;
G面经prepare: Sort String Based On Another的更多相关文章
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- 791. Custom Sort String - LeetCode
Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 A) 9 3 4 # # 1 # ...
- Groupon面经Prepare: Sort given a range && Summary: Bucket Sort
首先是如何sort一个只有0和1的数组,要求inplace. follow up是告诉一个range,如何在O(N)时间内sort好 两个pointer可解 package Sorting; impo ...
- 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 ...
- [LeetCode] Custom Sort String 自定义排序的字符串
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
- 牛客第三场多校 E Sort String
链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...
- 791. Custom Sort String
S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...
随机推荐
- Gradient
https://en.wikipedia.org/wiki/Gradient
- java时间格式串
yyyy-mm-dd 和yyyy-MM-dd转换出来的日期不用. 用"yyyy-MM-dd"
- php经典笔试题
五.基础及程序题(建议使用你擅长的语言:C/C++.PHP.Java) 5.写一个排序算法,可以是冒泡排序或者是快速排序,假设待排序对象是一个维数组.(提示:不能使用系统已有函数,另外请仔细回忆以前学 ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(9)
Android 4.3以后的系统自动支持蓝牙4.0规范的低功耗蓝牙(BLE).在android4.3之前,蓝牙4.0支持是由手机厂家加入支持的,接口各异,导致开发一个支持蓝牙4.0程序支持市面上的手机 ...
- BLE-NRF51822教程17-DFU使用手机升级
演示的工程是 [application] nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble_app_hrs\pca10028\s110_w ...
- 1014 C语言文法
<程序> -> <外部声明> | <程序> <外部声明> <外部声明> -> <函数定义> | <声明> ...
- UNION 查询中的排序
MSSQL 不允许在UNION查询中使用 ORDER BY 因此,当我们需要这种功能的时候,就需要绕一些弯路. 比如有一张学生表student 和教师表 teacher , 我们要查询所有的教师学生的 ...
- Qt 动画快速入门(一)
Qt-4.6动画Animation快速入门三字决 Qt-4.6新增了Animation Framework(动画框架),让我们能够方便的写一些生动的程序.不必像以前的版本一样,所有的控件都枯燥的呆在伟 ...
- RTSP交互命令简介及过程参数描述
目录 [hide] 1 RTSP消息格式 2 简单的rtsp交互过程 3 rtsp中常用方法 3.1 OPTION 3.2 DESCRIBE 3.3 SETUP 3.4 PLAY 3.5 PAUSE ...
- Android 开发-Intent传递普通字符串
假设A传递id到B中 ActivityA: Intent intent=new Intent(); intent.setClass(ActivityA.this,ActivityB.class) ...