-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcustomSortString.java
More file actions
51 lines (48 loc) · 1.6 KB
/
customSortString.java
File metadata and controls
51 lines (48 loc) · 1.6 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
51
import java.util.*;
public class customSortString {
public static void main(String[] args){
String S = "cda";
String T = "abcd";
System.out.println(customSortString(S,T));
Set<String> set = new HashSet<>();
List<String> list = new ArrayList<>();
set.addAll(list);
}
public static String customSortString(String S, String T) {
/*
my idea is 1. first, find all chars in T which appear in S
2. sort them
3. put sorted elements into String S
*/
Set<Character> set = new HashSet<>();
for(char c : S.toCharArray()){
set.add(c);
}
// use an int array with 26 length to store chars in T
int[] appearedChars = new int[26];
char[] tChars = T.toCharArray();
for(char e : tChars){
if(set.contains(e)){
appearedChars[e-'a'] ++;
}
}
System.out.println(Arrays.toString(appearedChars));
// sort the appeared characters and store them into a stack
Stack<Character> stack = new Stack<>();
char[] sChars = S.toCharArray();
for(int i = sChars.length-1; i >= 0; i--){
char cur = sChars[i];
for(int j=0; j<appearedChars[cur-'a']; j ++){
stack.push(cur);
}
}
System.out.println(stack);
// put sorted elements into T again
for(int i=0; i < tChars.length; i ++){
if(set.contains(tChars[i])){
tChars[i] = stack.pop();
}
}
return String.valueOf(tChars);
}
}