791. Custom Sort String, 1122. Relative Sort Array
T: O(nlogn)
S: O(n)
```java
class Solution {
public String customSortString(String order, String s) {
int[] ord = new int[26];
Arrays.fill(ord, 26); // max is 26
for (int i = 0; i < order.length(); i++) {
ord[order.charAt(i) - 'a'] = i;
}
List<Character> cc = new ArrayList<>();
for (char ch : s.toCharArray()) {
cc.add(ch);
}
Collections.sort(cc, (a, b) -> {
return ord[a - 'a'] - ord[b - 'a'];
});
StringBuilder result = new StringBuilder();
for (char c : cc) {
result.append(c);
}
return result.toString();
}
}
/**
using fixing order for cba
*/
```Last updated
Was this helpful?