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 |
abbc
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Lexicographically all Shortest Palindromic Substrings from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Check if permutation of a given string can be made palindromic by removing at most K characters
- Make palindromic string non-palindromic by rearranging its letters
- Count of Palindromic substrings in an Index range
- Count all Prime Length Palindromic Substrings
- Count of K-size substrings having palindromic permutations
- Generate a String of having N*N distinct non-palindromic Substrings
- Rearrange the string to maximize the number of palindromic substrings
- Check if a string can be split into even length palindromic substrings
- Split string into three palindromic substrings with earliest possible cuts
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Find the count of palindromic sub-string of a string in its sorted form
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.

