Permutation of given string that maximizes count of Palindromic substrings Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string S, the task is to find the permutation of the string such that palindromic substrings in the string are maximum.Note: There can be multiple answers for each string. Examples: Input: S = "abcb" Output: "abbc" Explanation: "abbc" is the string with maximum number of palindromic substrings. Palindromic Substrings are - {"a", "b", "b", "c", "abbc"}Input: S = "oolol" Output: "ololo" Approach: The idea is to sort the characters of the string such that individually and together form a palindromic substring which will maximize the total palindromic substring possible for the permutation of the string.Below is the implementation of the above approach: C++ // C++ implementation to find the // permutation of the given string // such that palindromic substrings // is maximum in the string #include <bits/stdc++.h> using namespace std; // Function to find the permutation // of the string such that the // palindromic substrings are maximum string maxPalindromicSubstring(string s){ // Sorting the characters of the // given string sort(s.begin(), s.end()); cout << s; return s; } // Driver Code int main() { // String s string s = "abcb"; // Function Call maxPalindromicSubstring(s); return 0; } Java // Java implementation to find the // permutation of the given string // such that palindromic substrings // is maximum in the string import java.io.*; import java.util.*; class GFG { // Function to find the permutation // of the string such that the // palindromic substrings are maximum static String maxPalindromicSubstring(String s) { // Convert input string to char array char tempArray[] = s.toCharArray(); // Sorting the characters of the // given string Arrays.sort(tempArray); System.out.println(tempArray); // Return new sorted string return new String(tempArray); } // Driver code public static void main(String[] args) { // String s String s = "abcb"; // Function Call maxPalindromicSubstring(s); } } // This code is contributed by coder001 Python3 # Python3 implementation to find the # permutation of the given string # such that palindromic substrings # is maximum in the string # Function to find the permutation # of the string such that the # palindromic substrings are maximum def maxPalindromicSubstring(s): # Sorting the characters of the # given string res = ''.join(sorted(s)) s = str(res) print(s) # Driver Code if __name__ == '__main__': # String s s = "abcb" # Function Call maxPalindromicSubstring(s) # This code is contributed by BhupendraSingh C# // C# implementation to find the // permutation of the given string // such that palindromic substrings // is maximum in the string using System; class GFG{ // Function to find the permutation // of the string such that the // palindromic substrings are maximum static String maxPalindromicSubstring(String s) { // Convert input string to char array char []tempArray = s.ToCharArray(); // Sorting the characters of the // given string Array.Sort(tempArray); Console.WriteLine(tempArray); // Return new sorted string return new String(tempArray); } // Driver code public static void Main() { // String s String s = "abcb"; // Function Call maxPalindromicSubstring(s); } } // This code is contributed by sapnasingh4991 JavaScript <script> // Javascript implementation to find the // permutation of the given string // such that palindromic substrings // is maximum in the string // Function to find the permutation // of the string such that the // palindromic substrings are maximum function maxPalindromicSubstring(s){ // Sorting the characters of the // given string s.sort(); document.write(s.join("")) return s; } // Driver Code // String s var s = "abcb".split(''); // Function Call maxPalindromicSubstring(s); // This code is contributed by noob2000. </script> Output: abbc Time Complexity: O(n*log(n)) where n is the size of the string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find the count of palindromic sub-string of a string in its sorted form Y yash2040 Follow Improve Article Tags : Strings Sorting DSA palindrome permutation substring cpp-strings +3 More Practice Tags : cpp-stringspalindromepermutationSortingStrings +1 More Similar Reads Count of K-size substrings having palindromic permutations Given string str consists of only lowercase alphabets and an integer K, the task is to count the number of substrings of size K such that any permutation of the substring is a palindrome. Examples: Input: str = "abbaca", K = 3 Output: 3 Explanation: The substrings of size 3 whose permutation is pali 9 min read Maximum even length sub-string that is permutation of a palindrome Given string str , the task is to find the maximum length of the sub-string of str that can be arranged into a Palindrome (i.e at least one of its permutation is a Palindrome). Note that the sub-string must be of even length. Examples: Input: str = "124565463" Output: 6 "456546" is the valid sub-str 9 min read Maximum even length sub-string that is permutation of a palindrome Given string str , the task is to find the maximum length of the sub-string of str that can be arranged into a Palindrome (i.e at least one of its permutation is a Palindrome). Note that the sub-string must be of even length. Examples: Input: str = "124565463" Output: 6 "456546" is the valid sub-str 9 min read Find the count of palindromic sub-string of a string in its sorted form Given string str consisting of lowercase English alphabets, the task is to find the total number of palindromic sub-strings present in the sorted form of str. Examples: Input: str = "acbbd" Output: 6 All palindromic sub-string in it's sorted form ("abbcd") are "a", "b", "b", "bb", "c" and "d". Input 5 min read Find the count of palindromic sub-string of a string in its sorted form Given string str consisting of lowercase English alphabets, the task is to find the total number of palindromic sub-strings present in the sorted form of str. Examples: Input: str = "acbbd" Output: 6 All palindromic sub-string in it's sorted form ("abbcd") are "a", "b", "b", "bb", "c" and "d". Input 5 min read 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 a 5 min read Like