Skip to content

Commit f4a1ffd

Browse files
committed
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)
1 parent 18cba9b commit f4a1ffd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

July2021/CustomSortString.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
// O(Len(Str + Order))
3+
public String customSortString(String order, String str) {
4+
int[] freq = new int[26];
5+
6+
// TC : O(len of Str)
7+
for(char c : str.toCharArray()){
8+
freq[c-'a']++;
9+
}
10+
11+
StringBuilder ans = new StringBuilder();
12+
13+
// TC : O(Len(orderStr + Str))
14+
for(char orderChar: order.toCharArray()) {
15+
16+
while(freq[orderChar-'a']>0) {
17+
ans.append(orderChar);
18+
freq[orderChar-'a']--;
19+
}
20+
}
21+
22+
// TC : O(Len(Str))
23+
for(int i=0;i<26;i++){
24+
int freqC = freq[i];
25+
while(freqC>0) {
26+
ans.append((char)(i+'a'));
27+
freqC--;
28+
}
29+
}
30+
return ans.toString();
31+
}
32+
}

July2021/README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
1. [FindMedianfromDataStream - Day 11](/July2021/FindMedianfromDataStream.java)
1616
1. [IsomorphicStrings - Day 12](/July2021/IsomorphicStrings.java)
1717
1. [FindPeakElement - Day 13](/July2021/FindPeakElement.java)
18+
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ It is a place where peers of the community help each other with their queries, s
9393
1. [FindMedianfromDataStream - Day 11](/July2021/FindMedianfromDataStream.java)
9494
1. [IsomorphicStrings - Day 12](/July2021/IsomorphicStrings.java)
9595
1. [FindPeakElement - Day 13](/July2021/FindPeakElement.java)
96+
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)
9697

9798
### [June 2021 Leetcode Challenge](https://leetcode.com/explore/featured/card/june-leetcoding-challenge-2021/)
9899

0 commit comments

Comments
 (0)