forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_791.java
32 lines (30 loc) · 966 Bytes
/
_791.java
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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _791 {
public static class Solution1 {
public String customSortString(String S, String T) {
Map<Character, Integer> map = new HashMap<>();
for (char c : T.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
StringBuilder sb = new StringBuilder();
for (char c : S.toCharArray()) {
if (map.containsKey(c)) {
int count = map.get(c);
while (count-- > 0) {
sb.append(c);
}
map.remove(c);
}
}
for (char c : map.keySet()) {
int count = map.get(c);
while (count-- > 0) {
sb.append(c);
}
}
return sb.toString();
}
}
}