📌  相关文章
📜  字符串的字符重新排列,使其回文子的串联

📅  最后修改于: 2021-04-17 17:58:48             🧑  作者: Mango

给定由小写字母组成的字符串S ,任务是检查给定的字符串是否可以重新排列,以使该字符串可以拆分为长度至少为2的不重叠的回文子字符串。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:可以通过将字符串的字符重新排列为长度为2的子字符串(由单个不同的字符组成)来解决给定的问题。如果存在具有奇数频率的字符,则将其放置在长度为2的回文子串的中间。
请按照以下步骤解决问题:

  • 初始化一个辅助数组,例如,size [ 26 ]frequency [] ,以存储出现在字符串S中的每个字符的频率。
  • 遍历给定的字符串S并更新数组frequency []中每个字符的频率
  • 初始化两个变量,将奇数偶数均设置为0 ,以存储奇数元素的频率和形成的长度为2的回文子串数。
  • 遍历数组frequency [] ,如果frequency [i]的值大于0 ,则将值[frequency [i]&1]和[frequency [i] / 2]分别添加到变量奇数偶数
  • 完成上述步骤后,如果奇数的值最多为偶数,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if a string can be
// modified such that it can be split into
// palindromic substrings of length >= 2
void canSplit(string& S)
{
    // Stores frequencies of characters
    vector frequency(26, 0);
 
    int cnt_singles = 0;
 
    int k = 0;
 
    // Traverse the string
    for (int i = 0; i < S.length(); i++)
 
        // Update frequency
        // of each character
        frequency[S[i] - 'a']++;
 
    int odd = 0, eve = 0;
 
    // Traverse the frequency array
    for (int i = 0; i < 26; i++) {
 
        // Update values of odd and eve
        if (frequency[i]) {
            odd += (frequency[i] & 1);
            eve += frequency[i] / 2;
        }
    }
 
    // Print the result
    if (eve >= odd)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    string S = "aaabbbccc";
    canSplit(S);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to check if a string can be
    // modified such that it can be split into
    // palindromic substrings of length >= 2
    static void canSplit(String S)
    {
        // Stores frequencies of characters
        int frequency[] = new int[26];
 
        int cnt_singles = 0;
 
        int k = 0;
 
        // Traverse the string
        for (int i = 0; i < S.length(); i++)
 
            // Update frequency
            // of each character
            frequency[S.charAt(i) - 'a']++;
 
        int odd = 0, eve = 0;
 
        // Traverse the frequency array
        for (int i = 0; i < 26; i++) {
 
            // Update values of odd and eve
            if (frequency[i] != 0) {
                odd += (frequency[i] & 1);
                eve += frequency[i] / 2;
            }
        }
 
        // Print the result
        if (eve >= odd)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        String S = "aaabbbccc";
        canSplit(S);
    }
}


Python3
# Python3 program for the above approach
 
# Function to check if a string can be
# modified such that it can be split into
# palindromic substrings of length >= 2
def canSplit(S):
 
    # Stores frequencies of characters
    frequency = [0] * 26
 
    cnt_singles = 0
 
    k = 0
 
    # Traverse the string
    for i in range(len(S)):
 
        # Update frequency
        # of each character
        frequency[ord(S[i]) - ord('a')] += 1
 
    odd = 0
    eve = 0
 
    # Traverse the frequency array
    for i in range(26):
 
        # Update values of odd and eve
        if (frequency[i]):
            odd += (frequency[i] & 1)
            eve += frequency[i] // 2
 
    # Print the result
    if (eve >= odd):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == "__main__" :
 
    S = "aaabbbccc"
     
    canSplit(S)
 
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to check if a string can be
  // modified such that it can be split into
  // palindromic substrings of length >= 2
  static void canSplit(string S)
  {
 
    // Stores frequencies of characters
    int []frequency = new int[26];
    int cnt_singles = 0;
    int k = 0;
 
    // Traverse the string
    for (int i = 0; i < S.Length; i++)
 
      // Update frequency
      // of each character
      frequency[S[i] - 'a']++;
 
    int odd = 0, eve = 0;
 
    // Traverse the frequency array
    for (int i = 0; i < 26; i++)
    {
 
      // Update values of odd and eve
      if (frequency[i] != 0)
      {
        odd += (frequency[i] & 1);
        eve += frequency[i] / 2;
      }
    }
 
    // Print the result
    if (eve >= odd)
      Console.WriteLine("Yes");
    else
      Console.WriteLine("No");
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    string S = "aaabbbccc";
    canSplit(S);
  }
}
 
// This code is contributed by AnkThon


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)