Custom Sort String LeetCode solution in Java

1
class Solution {
2
public String customSortString(String X, String Y) {
3
// char count of string Y
4
int[] charCount = new int[26];
5
for (char c : Y.toCharArray()) {
6
charCount[c - 'a']++;
7
}
8

9
StringBuilder sb = new StringBuilder();
10

11
// first store char in same order of String X
12
for (char c : X.toCharArray()) {
13
while (charCount[c - 'a']-- > 0) {
14
sb.append(c);
15
}
16
}
17

18
// now store remaining char of string Y
19
for (int i = 0; i < 26; i++) {
20
char c = (char) ('a' + i);
21
while (charCount[i]-- > 0) {
22
sb.append(c);
23
}
24
}
25

26
return sb.toString();
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0