File tree Expand file tree Collapse file tree 1 file changed +11
-21
lines changed Expand file tree Collapse file tree 1 file changed +11
-21
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public String customSortString (String S , String T ) {
3
- Set <Character > set = new HashSet <>();
4
- for (int i = 0 ; i < S .length (); i ++) {
5
- set .add (S .charAt (i ));
6
- }
2
+ public String customSortString (String order , String s ) {
7
3
Map <Character , Integer > map = new HashMap <>();
8
- StringBuilder sb = new StringBuilder ();
9
- for (char c : T .toCharArray ()) {
10
- if (set .contains (c )) {
11
- map .put (c , map .getOrDefault (c , 0 ) + 1 );
12
- }
13
- else {
14
- sb .append (c );
15
- }
16
- }
17
- for (int i = 0 ; i < S .length (); i ++) {
18
- char c = S .charAt (i );
19
- int count = map .getOrDefault (c , 0 );
20
- while (count -- > 0 ) {
21
- sb .append (c );
22
- }
4
+ int position = 1 ;
5
+ for (char c : order .toCharArray ()) {
6
+ map .put (c , position ++);
23
7
}
24
- return sb .toString ();
8
+ return s .chars ()
9
+ .mapToObj (c -> (char ) c )
10
+ .sorted (Comparator .comparing (o -> map .getOrDefault (o , 0 )))
11
+ .collect (Collector .of (StringBuilder ::new ,
12
+ StringBuilder ::append ,
13
+ StringBuilder ::append ,
14
+ StringBuilder ::toString ));
25
15
}
26
16
}
You can’t perform that action at this time.
0 commit comments