# HackerEarth Sorted String problem solution

In this HackerEarth Sorted String problem solution Little Ashish got a lot of strings as his birthday gift. He does not mind getting so many strings for free; in fact, he loves them. But, on noticing all the strings he received as a gift, Little Ashish, who's also a snob and a bit OCD kind of a guy, realizes that he does not like the way in which the strings are arranged.

He likes his strings sorted, in a different kind of a way. He wants his strings to be sorted based on the count of characters present in the string. For instance, if the string is: "aaabbc", then the desired string would be: cbbaaa. In case where the count of two characters is same, then the lexicographically smaller one will be printed first. For instance: "aabbcc" then, the output will be: "aabbcc".

## HackerEarth Sorted String problem solution.

`#include <bits/stdc++.h>using namespace std;#define f_in(st) freopen(st,"r",stdin);#define f_out(st) freopen(st,"w",stdout);int main(){  //f_in("in09.txt");  //f_out("out09.txt");  int test;  cin>>test;  while(test--)  {     string s;    cin>>s;    vector<pair<int, int> > V(26);    for(int i=0;i<26;i++){      V[i].first=0;      V[i].second=i;    }    for(int i=0;i<s.size();i++)      V[s[i]-'a'].first++;    sort(V.begin(),V.end());    for(int i=0;i<26;i++)    {      int count=V[i].first;      char ch=V[i].second+'a';      for(int j=0;j<count;j++)        cout<<ch;    }    cout<<endl;  }  return 0;}`

### Second solution

`#include<bits/stdc++.h>using namespace std;#define vi vector < int >#define pii pair < int , int >#define pb push_back#define mp make_pair#define ff first#define ss second#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )#define ll long long#define llu unsigned long long#define MOD 1000000007#define INF 0x3f3f3f3f#define dbg(x) { cout<< #x << ": " << (x) << endl; }#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }#define all(x) x.begin(),x.end()#define mset(x,v) memset(x, v, sizeof(x))#define sz(x) (int)x.size()char s;int c;bool cmp(char a,char b){    if(c[a-'a'] == c[b-'a'])        return a < b;    return c[a-'a'] < c[b-'a'];}int main(){    int t,i;    scanf("%d",&t);    assert(1 <= t && t <= 100);    while(t--)    {        scanf("%s",s);        mset(c,0);        for(i=0;s[i];i++)        {            assert('a' <= s[i] && s[i] <= 'z');            c[s[i]-'a']++;        }        int n = i;        assert(1 <= n && n <= 100);        sort(s,s+n,cmp);        printf("%s\n",s);    }    return 0;}`