Difficult Characters

Yesterday while Omar was trying to learn English, he saw that there are letters repeated many times in words while some other letters repeated only a few times or not repeated at all!

Of course anyone can memorize the letters (repeated many times) better than the letters repeated few times, so Omar will concatenate all the words in the context he has, and try to know the difficulty of each letter according to the number of repetitions of each letter.

So Omar has now the whole context and wants to arrange the letters from the most difficult letter (repeated few times) to the less difficult letter (repeated many times).

If there are 2 letters with the same level of difficulty, the letter with higher value of ASCII code will be more difficult.

Example:

Input:  s = "oomar"
Output: z y x w v u t s q p n l k j i h g f e d c b r m a o 

Approach

C++

#include <bits/stdc++.h>
using namespace std;

bool cmp(pair<charintapair<charintb)
{
    if (a.second == b.second)
        return a.first > b.first;
    return a.second < b.second;
}

void difficultCharacters(string s)
{

    int f[26] = {0};

    for (int i = 0i < s.size(); i++)
        f[s[i] - 'a']++;
    vector<pair<charint>> v;
    for (int i = 0i < 26i++)
        v.push_back({i + 'a'f[i]});
    sort(v.begin(), v.end(), cmp);
    for (int i = 0i < v.size(); i++)
        cout << v[i].first << " ";
    cout << "\n";
}
int main()
{

    string s = "oomar";

    difficultCharacters(s);

    return 0;
}


No comments:

Post a Comment