diff --git a/791. Custom Sort String b/791. Custom Sort String new file mode 100644 index 0000000..5ab203f --- /dev/null +++ b/791. Custom Sort String @@ -0,0 +1,23 @@ +class Solution { +public: + string customSortString(string order, string s) { + unordered_map sCharFreqMp; + for(auto currChr : s)sCharFreqMp[currChr]++; + string res = ""; + + for(auto currChr : order){ + if(sCharFreqMp.find(currChr) != sCharFreqMp.end()){ + int freq = sCharFreqMp[currChr]; + while(freq-->0)res+= currChr; + sCharFreqMp[currChr] = 0; + } + } + for(auto currChr : s){ + if(sCharFreqMp[currChr] != 0){ + res += currChr; + sCharFreqMp[currChr]--; + } + } + return res; + } +};