Custom Sort String
给一个字符串是序列的order, 求排序一个字符串.
class Solution {
public String customSortString(String order, String s) {
Map<Character, Integer> m = new HashMap<>();
int k = 100;
for(char c : order.toCharArray())
m.put(c, k--);
List<Character> list = new ArrayList<>();
for(char c : s.toCharArray())
list.add(c);
Collections.sort(list, (a,b) ->{
int aa = m.getOrDefault(a, Integer.MAX_VALUE / 2);
int bb = m.getOrDefault(b, Integer.MAX_VALUE / 2);
return bb - aa;
});
StringBuilder sb = new StringBuilder();
for(char c : list)
sb.append(c);
return sb.toString();
}
}