-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
26 lines (21 loc) · 763 Bytes
/
solution.java
File metadata and controls
26 lines (21 loc) · 763 Bytes
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
class Solution {
public String customSortString(String order, String s) {
//Count chars from string
int[] count = new int[26];
for (char c: s.toCharArray())
count[c - 'a']++;
StringBuilder ans = new StringBuilder();
//Add chars from Order string
//Inplace of original order chars
for (char c: order.toCharArray()) {
for (int i = 0; i < count[c - 'a']; ++i)
ans.append(c);
count[c - 'a'] = 0;
}
//Add rest of the chars from string
for (char c = 'a'; c <= 'z'; ++c)
for (int i = 0; i < count[c - 'a']; ++i)
ans.append(c);
return ans.toString();
}
}