791. Custom Sort String | 2 Approaches | Custom Comparator | Hash Map

preview_player
Показать описание
In this video, I'll talk about how to solve Leetcode 791. Custom Sort String | Custom Comparator | Hash Map | Meta

Let's Connect:

About Me:
I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)

✨ Timelines✨

✨ Hashtags ✨
#programming #Interviews #leetcode #faang #maang #datastructures #algorithms
Рекомендации по теме
Комментарии
Автор

I guess Leetcode Easy days are gone, this week is gonna be Medium & next Hard 🫡🧑🏻‍🦯

ARYANMITTAL
Автор

bro used C++17 all of a sudden, I didnt even knew that structured binding part you used in the second for loop for collecting remaining elements. For those who didnt knew, here is what I understood:

for(auto [el, fr] : freq) is an example of "structured binding" introduced in C++17 that allows you to decompose a tuple-like object (such as a pair) into its individual components. So that In each iteration, the loop extracts a key-value pair (character-frequency pair) from the freq map.
el represents the key (character), and fr represents the value (frequency) corresponding to that key in the map.

DarthVader
Автор

Aryan sir please u mentioned if the order of rest elements need to be same then what changes have to be made ?

dhruvrawatt
Автор

Bro provide code link also cuz I do dsa using java. So sometimes it's helpful to understand the code more clearly in java.

AnshuHarshit
Автор

Really good video bro! I have a question tho, why does the second algorithm has a O(1) space complexity? If you created a new string called ans, shouldn't it be O(n) ?

paulodim
Автор

bhai opening line bdia to bhot lagi par chai and code ki jaise lag rhi hai🤣🤣🤣🤣

mukulkhanna
Автор

class Solution {
public:
string customSortString(string order, string s) {
vector<int>freq1(26, 0);
vector<int>freq2(26, 0);
for(char c:order) freq1[c-'a']++;
for(char c:s) freq2[c-'a']++;
string ans;
for(int i=0;i<order.size();i++){
while(freq2[order[i]-'a']){
ans.push_back(order[i]);
freq2[order[i]-'a']--;
}
}
for(int i=0;i<s.size();i++){
if(freq2[s[i]-'a']){
ans.push_back(s[i]);
}
}
return ans;
}
};

Engineering.Wallah