Custom Sort String - LeetCode Daily Challenge
Problem Statement
You are given two strings order
and s
. All the characters of order
are unique and were sorted in some custom order previously.
Permute the characters of s
so that they match the order that order
was sorted. More specifically, if a character x
occurs before a character y
in order
, then x
should occur before y
in the permuted string.
Return any permutation of s
that satisfies this property.
Example 1
Input: order = "cba", s = "abcd"
Output: "cbad"
Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".
Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.
Example 2
Input: order = "bcafg", s = "abcd"
Output: "bcad"
Explanation: The characters "b", "c", and "a" from order dictate the order for the characters in s. The character "d" in s does not appear in order, so its position is flexible.
Following the order of appearance in order, "b", "c", and "a" from s should be arranged as "b", "c", "a". "d" can be placed at any position since it's not in order. The output "bcad" correctly follows this rule. Other arrangements like "bacd" or "bcda" would also be valid, as long as "b", "c", "a" maintain their order.
Try here before watching the video.
Video Solution
Java Code
class Solution {
public String customSortString(String order, String s) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for(char ch : s.toCharArray()) {
frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
}
StringBuilder str = new StringBuilder();
for(char ch : order.toCharArray()) {
if(frequencyMap.containsKey(ch)) {
int frequency = frequencyMap.get(ch);
for(int i = 0; i < frequency; i++) {
str.append(ch);
frequencyMap.put(ch, frequencyMap.get(ch) - 1);
}
}
}
for(char ch : frequencyMap.keySet()) {
int frequency = frequencyMap.get(ch);
for(int i = 0; i < frequency; i++) {
str.append(ch);
}
}
return str.toString();
}
}
C++ Code
class Solution {
public:
string customSortString(string order, string s) {
unordered_map<char, int> frequencyMap;
for (char ch : s) {
frequencyMap[ch]++;
}
string str;
for (char ch : order) {
if (frequencyMap.count(ch)) {
int frequency = frequencyMap[ch];
for (int i = 0; i < frequency; i++) {
str += ch;
frequencyMap[ch]--;
}
}
}
for (auto& pair : frequencyMap) {
int frequency = pair.second;
for (int i = 0; i < frequency; i++) {
str += pair.first;
}
}
return str;
}
};
Python Code
class Solution:
def customSortString(self, order: str, s: str) -> str:
frequency_map = {}
for ch in s:
frequency_map[ch] = frequency_map.get(ch, 0) + 1
result = ''
for ch in order:
if ch in frequency_map:
frequency = frequency_map[ch]
for i in range(frequency):
result += ch
frequency_map[ch] -= 1
for ch, frequency in frequency_map.items():
for i in range(frequency):
result += ch
return result
Javascript Code
var customSortString = function(order, s) {
const frequencyMap = {};
for (const ch of s) {
frequencyMap[ch] = (frequencyMap[ch] || 0) + 1;
}
let str = '';
for (const ch of order) {
if (frequencyMap.hasOwnProperty(ch)) {
const frequency = frequencyMap[ch];
for (let i = 0; i < frequency; i++) {
str += ch;
frequencyMap[ch]--;
}
}
}
for (const ch in frequencyMap) {
const frequency = frequencyMap[ch];
for (let i = 0; i < frequency; i++) {
str += ch;
}
}
return str;
};
Go Code
func customSortString(order string, s string) string {
frequencyMap := make(map[rune]int)
for _, ch := range s {
frequencyMap[ch]++
}
var str strings.Builder
for _, ch := range order {
if frequency, ok := frequencyMap[ch]; ok {
for i := 0; i < frequency; i++ {
str.WriteRune(ch)
frequencyMap[ch]--
}
}
}
for ch, frequency := range frequencyMap {
for i := 0; i < frequency; i++ {
str.WriteRune(ch)
}
}
return str.String()
}
Complexity Analysis
Time Complexity: O(N), we are iterating over string s
and building frequencyMap
.
Space Complexity: O(N), frequencyMap
will take constant space as input strings contain lowercase letters only. But we are building an answer string; hence, it will take O(N).