Skip to content

Commit

Permalink
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunchit committed Jul 14, 2021
1 parent 18cba9b commit f4a1ffd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
32 changes: 32 additions & 0 deletions July2021/CustomSortString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
// O(Len(Str + Order))
public String customSortString(String order, String str) {
int[] freq = new int[26];

// TC : O(len of Str)
for(char c : str.toCharArray()){
freq[c-'a']++;
}

StringBuilder ans = new StringBuilder();

// TC : O(Len(orderStr + Str))
for(char orderChar: order.toCharArray()) {

while(freq[orderChar-'a']>0) {
ans.append(orderChar);
freq[orderChar-'a']--;
}
}

// TC : O(Len(Str))
for(int i=0;i<26;i++){
int freqC = freq[i];
while(freqC>0) {
ans.append((char)(i+'a'));
freqC--;
}
}
return ans.toString();
}
}
1 change: 1 addition & 0 deletions July2021/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
1. [FindMedianfromDataStream - Day 11](/July2021/FindMedianfromDataStream.java)
1. [IsomorphicStrings - Day 12](/July2021/IsomorphicStrings.java)
1. [FindPeakElement - Day 13](/July2021/FindPeakElement.java)
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ It is a place where peers of the community help each other with their queries, s
1. [FindMedianfromDataStream - Day 11](/July2021/FindMedianfromDataStream.java)
1. [IsomorphicStrings - Day 12](/July2021/IsomorphicStrings.java)
1. [FindPeakElement - Day 13](/July2021/FindPeakElement.java)
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)

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

Expand Down

0 comments on commit f4a1ffd

Please sign in to comment.