📌  相关文章
📜  给定字符串的排列使回文子串的数量最大化

📅  最后修改于: 2021-09-04 11:46:15             🧑  作者: Mango

给定一个字符串S ,任务是找到该字符串的排列,使得该字符串中的回文子字符串最大。
注意:每个字符串可以有多个答案。
例子:

方法:这个想法是对字符串的字符进行排序,使得单独和一起形成一个回文子串,这将最大化可能用于字符串排列的总回文子串。
下面是上述方法的实现:

C++
// C++ implementation to find the
// permutation of the given string
// such that palindromic substrings
// is maximum in the string
 
#include 
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


输出:
abbc

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live