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

📅  最后修改于: 2021-04-27 05:22:27             🧑  作者: 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


输出:
abbc