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 ...
随机推荐
- Mysql 定时备份操作
1.创建保存备份文件的路径/mysqldata #mkdir /bak/mysqlbak 2.创建/usr/sbin/bakmysql文件 #vi /usr/sbin/bakmysql.sh 3.写入 ...
- IN()
High Performance My SQL, Third Edition Consider the followingWHERE clause: WHERE eye_color IN('brown ...
- Mongo中的数据类型
一.null null用于表示空值或者不存在的字段 {"X" : null} 二.布尔型 布尔类型有两个值true和false {"x" : true} 三.数 ...
- 欢迎大家提问Android技术及职业生涯等问题
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 最近有些时间,但QQ群问的问题比较多,不能一一解答,如果有价值 ...
- yii2 实现多表联查
- SWT常用组件(转)
转载自:http://www.cnblogs.com/happyPawpaw/archive/2012/10/19/2730478.html 1按钮组件(Button) (1)Button组件常用样式 ...
- Linux some command(continue...)
挂载硬盘 sudo mount -t ext4 /dev/sdb1 /media/hadoop 自动挂载相关 sudo blkid sudo fdisk -l vim /etc/fstab cat / ...
- leetcode:Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. 最初的代码 class Solution { public: int t ...
- magento csv导入
- Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select
此文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,对下拉列表框 Select 的操作. 下拉列表是 Web UI 自动化测试过程中使用率非常高的,通常有两种形式的下拉列表,一 ...