-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path791.java
More file actions
50 lines (44 loc) · 1.56 KB
/
791.java
File metadata and controls
50 lines (44 loc) · 1.56 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public String customSortString(String S, String T) {
int[] counts = new int[26];
for(char c : T.toCharArray()){
counts[c - 'a']++;
}
StringBuilder res = new StringBuilder();
for(int i = 0; i<S.length(); i++){
while(counts[S.charAt(i) -'a']-- > 0){
res.append(S.charAt(i));
}
}
for(int i = 0; i<counts.length; i++){
while(counts[i]-- > 0){
res.append((char)(i + 'a'));
}
}
return res.toString();
}
}
__________________________________________________________________________________________________
sample 34308 kb submission
class Solution {
public String customSortString(String S, String T) {
int[] order = new int[26];
for (int i = 0; i < S.length(); i++) {
char c = S.charAt(i);
order[c-'a'] = i+1;
}
List<Character> li = new ArrayList<>();
for (int i = 0; i < T.length(); i++) {
li.add(T.charAt(i));
}
li.sort((a, b) -> (order[a-'a'] - order[b-'a']));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < li.size(); i++) {
sb.append(li.get(i));
}
return sb.toString();
}
}
__________________________________________________________________________________________________