-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL791.java
More file actions
26 lines (25 loc) · 1.04 KB
/
L791.java
File metadata and controls
26 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
class Solution791 {
class Solution {
/**
* 791. Custom Sort String https://leetcode.com/problems/custom-sort-string/description/
*
* @param S String
* A string representing order of characters for sorting
* @param T String
* Input string to sort based on given order
* @return Final result string
* @timeComplexity O(n log n) Where n is the length of String T
* @spaceComplexity O(1)
*/
public String customSortString(String S, String T) {
List<Character> chars = T.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
// We create a new comparator and sort the input string based on this comparator
Collections.sort(chars, Comparator.comparingInt(c -> S.indexOf(c)));
return chars.stream().map(String::valueOf).collect(Collectors.joining());
}
}
}