Sort a string according to the frequency of characters | C ++ implementation to Sort strings according to the frequency of characters in ascending order ; Returns count of character in the string ; Check for vowel ; Function to sort the string according to the frequency ; Vector to store the frequency of characters with respective character ; Inserting frequency with respective character in the vector pair ; Sort the vector , this will sort the pair according to the number of characters ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countFrequency ( string str , char ch ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str [ i ] == ch ) ++ count ; return count ; } void sortArr ( string str ) { int n = str . length ( ) ; vector < pair < int , char > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( countFrequency ( str , str [ i ] ) , str [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second ; } int main ( ) { string str = " geeksforgeeks " ; sortArr ( str ) ; return 0 ; }