forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. [CustomSortString - Day 14](/July2021/CustomSortString.java)
- Loading branch information
Showing
3 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters