Rearrange the string to maximize the number of palindromic substrings | C ++ program to rearrange the string such to maximize the number of palindromic substrings ; Function to return the newString ; length of string ; hashing array ; iterate and count ; resulting string ; form the resulting string ; number of times character appears ; append to resulting string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string newString ( string s ) { int l = s . length ( ) ; int freq [ 26 ] = { 0 } ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s [ i ] - ' a ' ] += 1 ; } string ans = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { ans += ( char ) ( 97 + i ) ; } } return ans ; } int main ( ) { string s = " aabab " ; cout << newString ( s ) ; return 0 ; }