# Rearrange the string to maximize the number of palindromic substrings

Given a string S consisting of lowercase characters(a-z) only, the task is to print a new string by rearranging the string in such a way that maximizes the number of palindromic substrings. In case of multiple answers, print any one.

Note: even if some substrings coincide, count them as many times as they appear in the obtained string.

Examples:

Input: s = “aabab”
Output: ababa
string “ababa” has 9 palindromic substrings: “a”, “b”, “a”, “b”, “a”, “aba”, “bab”, “aba”, “ababa”.

Input: s = “aa”
Output: aa
The given string has the maximum number of palindromic substrings possible, “a”, “a” and “aa”.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The problem might look to be a complex one but on solving for various cases and having observations will lead to an easy solution.

A simple solution is to sort the string and print it. Sorting takes O(N * log N).

An efficient solution is to count the frequency of each character using a freq[] array and then construct the string using the freq[] array.

Below is the implementation of the above approach.

## C++

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

## Java

 `// Java program to rearrange the ` `// string such to maximize the ` `// number of palindromic substrings ` ` `  ` `  `public` `class` `GFG { ` `     `  `    ``// Function to return the newString ` `    ``static` `String newString(String s) ` `    ``{ ` `        ``// length of string ` `        ``int` `l = s.length(); ` ` `  `        ``// hashing array ` `        ``int` `freq[] = ``new` `int` `[``26``] ; ` ` `  `        ``// iterate and count ` `        ``for` `(``int` `i = ``0``; i < l; i++) { ` `            ``freq[s.charAt(i) - ``'a'``] += ``1``; ` `        ``} ` ` `  `        ``// resulting string ` `        ``String ans = ``""``; ` ` `  `        ``// form the resulting string ` `        ``for` `(``int` `i = ``0``; i < ``26``; i++) { ` ` `  `            ``// number of times character appears ` `            ``for` `(``int` `j = ``0``; j < freq[i]; j++) { ` ` `  `                ``// append to resulting string ` `                ``ans += (``char``)(``97` `+ i); ` `            ``} ` `        ``} ` ` `  `        ``return` `ans; ` `    ``} ` ` `  ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `           ``String s = ``"aabab"``; ` `            ``System.out.println(newString(s)); ` `    ``} ` `    ``// This Code is contributed by ANKITRAI1 ` `} `

## Python3

 `# Python3 program to rearrange the  ` `# string such to maximize the  ` `# number of palindromic substrings  ` ` `  `# Function to return the newString  ` `def` `newString(s):  ` ` `  `    ``# length of string  ` `    ``l ``=` `len``(s)  ` ` `  `    ``# hashing array  ` `    ``freq ``=` `[``0``] ``*` `(``26``)  ` ` `  `    ``# iterate and count  ` `    ``for` `i ``in` `range``(``0``, l):  ` `        ``freq[``ord``(s[i]) ``-` `ord``(``'a'``)] ``+``=` `1` ` `  `    ``# resulting string  ` `    ``ans ``=` `""  ` ` `  `    ``# form the resulting string  ` `    ``for` `i ``in` `range``(``0``, ``26``):  ` ` `  `        ``# number of times character appears  ` `        ``for` `j ``in` `range``(``0``, freq[i]):  ` ` `  `            ``# append to resulting string  ` `            ``ans ``+``=` `chr``(``97` `+` `i)  ` `     `  `    ``return` `ans  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``s ``=` `"aabab"` `    ``print``(newString(s)) ` ` `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# program to rearrange the ` `// string such to maximize the ` `// number of palindromic substrings ` `using` `System; ` `class` `GFG ` `{ ` ` `  `// Function to return the newString ` `static` `String newString(``string` `s) ` `{ ` `    ``// length of string ` `    ``int` `l = s.Length; ` ` `  `    ``// hashing array ` `    ``int``[] freq = ``new` `int` `; ` ` `  `    ``// iterate and count ` `    ``for` `(``int` `i = 0; i < l; i++)  ` `    ``{ ` `        ``freq[s[i] - ``'a'``] += 1; ` `    ``} ` ` `  `    ``// resulting string ` `    ``string` `ans = ``""``; ` ` `  `    ``// form the resulting string ` `    ``for` `(``int` `i = 0; i < 26; i++) ` `    ``{ ` ` `  `        ``// number of times character appears ` `        ``for` `(``int` `j = 0; j < freq[i]; j++) ` `        ``{ ` ` `  `            ``// append to resulting string ` `            ``ans += (``char``)(97 + i); ` `        ``} ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `s = ``"aabab"``; ` `    ``Console.Write(newString(s)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by ChitraNayal `

Output:

```aaabb
```

Time Complexity: O(N)
Auxiliary Space: O(26) My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.