-
Notifications
You must be signed in to change notification settings - Fork 0
/
Case-specific Sorting of Strings
38 lines (34 loc) · 1.18 KB
/
Case-specific Sorting of Strings
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
class Solution {
//Function to perform case-specific sorting of strings.
public static String caseSort(String str) {
int[] freqLowerCase = new int[26];
int[] freqUpperCase = new int[26];
StringBuilder res = new StringBuilder();
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
freqLowerCase[str.charAt(i) - 'a']++;
else
freqUpperCase[str.charAt(i) - 'A']++;
}
int indexi = 0, indexj = 0;
for(int i = 0; i < str.length(); i++) {
if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
char x = 'a';
while(freqLowerCase[indexi] == 0) {
indexi++;
}
res.append(x += indexi);
freqLowerCase[indexi]--;
}
else{
char x = 'A';
while(freqUpperCase[indexj] == 0) {
indexj++;
}
res.append(x += indexj);
freqUpperCase[indexj]--;
}
}
return res.toString();
}
}