791. Custom Sort String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public String customSortString(String order, String s) {
int[] orders = new int[26];
Arrays.fill(orders, Integer.MAX_VALUE);
for (int i = 0; i < order.length(); i++) {
char c = order.charAt(i);
orders[c - 'a'] = i;
}

Character[] characters = new Character[s.length()];
for (int i = 0; i < s.length(); i++) {
characters[i] = s.charAt(i);
}
Arrays.sort(characters, Comparator.comparingInt(o -> orders[o - 'a']));

char[] chars = new char[characters.length];
for (int i = 0; i < chars.length; i++) {
chars[i] = characters[i];
}
return new String(chars);
}
}

References

791. Custom Sort String