Rearrange the string to maximize the number of palindromic substrings | Java 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
public class GFG { static String newString ( String s ) { int l = s . length ( ) ; int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( 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 ; } public static void main ( String args [ ] ) { String s = " aabab " ; System . out . println ( newString ( s ) ) ; } }