Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
620 views
in Online Assessments by Expert (46,090 points) | 620 views

3 Answers

0 like 0 dislike
Best answer
Minimum deletions required to make frequency of each letter unique
Input: "aabbffddeaee"
Output: 6
Input: "example"
Output: 4
by Expert (46,090 points)
0 like 0 dislike

Worked on those examples

 

def makeunique(str):

 

d = dict()
for x in str:
    if x in d:
        d[x]+=1
    else:
        d[x] = 1
c = sorted(d.items(),key = lambda x:-x[1])
cur = c[0][1] -1
out = 0

for freq in c[1:]:
    if cur == 0:
        out+=freq[1]
    elif freq[1] == cur:
        cur -=1
    elif freq[1] > cur:
        out+= freq[1] - cur
        cur -= 1
    else:
        cur = freq[1] - 1
return out
by Expert (46,090 points)
0 like 0 dislike

C++ Code:

 

    int minDeletions(string s) {
        vector<int> mp(26, 0);
        for(char c:s){
            mp[c-'a']++;
        }
        sort(mp.begin(), mp.end());
        int cnt=0;
        unordered_set<int> st;
        for(int i=mp.size()-1; i>=0;){
            if(mp[i]>0 && st.find(mp[i])!=st.end()){
                mp[i]--;
                cnt++;
            }else{
                st.insert(mp[i]);
                i--;
            }
        }
        return cnt;
    }

 

by Expert (46,090 points)