📌  相关文章
📜  将字符串分成相等的部分,以便所有部分都是回文

📅  最后修改于: 2021-06-26 22:23:30             🧑  作者: Mango

给定字符串str ,任务是将字符串分成最小部分,以使每个部分具有相同的长度,并且每个部分都是回文。打印所需数量的零件。

例子:

方法:

  • 计算出现的奇数字符并将其存储在countOdd中
  • 对所有偶数出现的字符的频率求和,并将其存储在sumEven中
  • 由于同一回文的一部分不能超过一个奇数频率字符。因此,如果(sumEven / 2)%countOdd = 0,则答案为countOdd,因为sumEven可分为countOdd部分。
  • 另外,我们仍然可以将出现的奇数字符分成回文对。例如,如果字符a出现3次,即aaa,aa可以是某些回文集的一部分,而保留a为奇数(不影响原始频率)。

下面是上述方法的实现:

CPP
// C++ implementation of the approach 
#include 
using namespace std;
  
// Function to return the frequency array 
// for the given string 
int* getFrequencies(string str) 
{ 
    static int freq[26] = { 0 };
  
    for (int i = 0; i < str.length(); i++) { 
        freq[str[i] - 'a']++; 
    } 
  
    return freq; 
} 
  
// Function to return the required count 
int countMinParts(string str) 
{ 
  
    int n = str.length(); 
    int *freq = getFrequencies(str); 
  
    vector oddFreq, evenFreq;
  
    int i, sumEven = 0; 
  
    for (i = 0; i < 26; i++) { 
        if (freq[i] == 0) 
            continue; 
  
        // Add frequencies of the even appearing 
        // characters 
        if (freq[i] % 2 == 0) 
            evenFreq.push_back(freq[i]); 
  
        // Count of the characters that appeared 
        // odd number of times 
        else
            oddFreq.push_back(freq[i]); 
    } 
  
    for (i = 0; i < evenFreq.size(); i++) { 
        sumEven += evenFreq[i]; 
    } 
  
    // If there are no characters with odd frequency 
    if (oddFreq.size() == 0) 
        return 1; 
  
    // If there are no characters with even frequency 
    if (sumEven == 0) { 
  
        // Only a single character with odd frequency 
        if (oddFreq.size() == 1) 
            return 1; 
  
        // More than 1 character with odd frequency 
        // string isn't a palindrome 
        return 0; 
    } 
  
    i = 0; 
  
    // All odd appearing characters can also contribute to 
    // the even length palindrome if one character 
    // is removed from the frequency leaving it as even 
    while (i < oddFreq.size()) { 
  
        // If k palindromes are possible where k 
        // is the number of characters with odd frequency 
        if ((sumEven / 2) % oddFreq.size() == 0) 
            return oddFreq.size(); 
  
        // Current character can no longer be an element 
        // in a string other than the mid character 
        if (oddFreq[i] == 1) { 
            i++; 
            continue; 
        } 
  
        // If current character has odd frequency > 1 
        // take two characters which can be used in 
        // any of the parts 
        sumEven += 2; 
  
        // Update the frequency 
        oddFreq[i] = oddFreq[i] - 2; 
    } 
  
    // If not possible, then every character of the 
    // string will act as a separate palindrome 
    return n; 
} 
  
// Driver code 
int main() 
{
  
    string s = "noonpeep"; 
  
    cout<


Java
// Java implementation of the approach
import java.util.*;
public class GFG {
  
    // Function to return the frequency array
    // for the given string
    static int[] getFrequencies(String str)
    {
        int freq[] = new int[26];
        for (int i = 0; i < str.length(); i++) {
            freq[str.charAt(i) - 'a']++;
        }
        return freq;
    }
  
    // Function to return the required count
    static int countMinParts(String str)
    {
  
        int n = str.length();
        int freq[] = getFrequencies(str);
        List oddFreq = new ArrayList<>();
        List evenFreq = new ArrayList<>();
  
        int i, sumEven = 0;
  
        for (i = 0; i < 26; i++) {
            if (freq[i] == 0)
                continue;
  
            // Add frequencies of the even appearing 
            // characters
            if (freq[i] % 2 == 0)
                evenFreq.add(freq[i]);
  
            // Count of the characters that appeared 
            // odd number of times
            else
                oddFreq.add(freq[i]);
        }
  
        for (i = 0; i < evenFreq.size(); i++) {
            sumEven += evenFreq.get(i);
        }
  
        // If there are no characters with odd frequency
        if (oddFreq.size() == 0)
            return 1;
  
        // If there are no characters with even frequency
        if (sumEven == 0) {
  
            // Only a single character with odd frequency
            if (oddFreq.size() == 1)
                return 1;
  
            // More than 1 character with odd frequency
            // string isn't a palindrome
            return 0;
        }
  
        i = 0;
  
        // All odd appearing characters can also contribute to
        // the even length palindrome if one character
        // is removed from the frequency leaving it as even
        while (i < oddFreq.size()) {
  
            // If k palindromes are possible where k
            // is the number of characters with odd frequency
            if ((sumEven / 2) % oddFreq.size() == 0)
                return oddFreq.size();
  
            // Current character can no longer be an element
            // in a string other than the mid character
            if (oddFreq.get(i) == 1) {
                i++;
                continue;
            }
  
            // If current character has odd frequency > 1
            // take two characters which can be used in
            // any of the parts
            sumEven += 2;
  
            // Update the frequency
            oddFreq.set(i, oddFreq.get(i) - 2);
        }
  
        // If not possible, then every character of the 
        // string will act as a separate palindrome
        return n;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        String s = "noonpeep";
  
        System.out.println(countMinParts(s));
    }
}
  
// This code is contributed by chitranayal


Python3
# Python3 implementation of the approach 
  
# Function to return the frequency array 
# for the given string 
def getFrequencies(string) :
        freq = [0] * 26
  
        for i in range(len(string)) :
                freq[ord(string[i]) - 
                     ord('a')] += 1
  
        return freq
  
# Function to return the required count 
def countMinParts(string) :
        n = len(string)
        freq = getFrequencies(string)
        oddFreq = []
        evenFreq = []
  
        sumEven = 0
  
        for i in range(26) :
  
                if freq[i] == 0 :
                    continue
  
                # Add frequencies of the even 
                # appearing characters 
                if freq[i] % 2 == 0 :
                        evenFreq.append(freq[i])
  
                # Count of the characters that 
                # appeared odd number of times
                else :
                        oddFreq.append(freq[i])
  
        for i in range(len(evenFreq)) :
                sumEven += evenFreq[i]
  
        # If there are no characters with
        # odd frequency 
        if len(oddFreq) == 0 :
                return 1
  
        # If there are no characters with 
        # even frequency
        if sumEven == 0 :
  
                # Only a single character with
                # odd frequency
                if len(oddFreq) == 1:
                        return 1
  
                # More than 1 character with odd 
                # frequency string isn't a palindrome 
                return 0
  
        i = 0
  
        # All odd appearing characters can also 
        # contribute to the even length palindrome 
        # if one character is removed from the 
        # frequency leaving it as even
        while(i < len(oddFreq)) :
                  
                # If k palindromes are possible where 
                # k is the number of characters with 
                # odd frequency 
                if ((sumEven / 2) % len(oddFreq) == 0) :
                        return len(oddFreq)
  
                # Current character can no longer be 
                # an element in a string other than
                # the mid character 
                if (oddFreq[i] == 1) :
                        i += 1
                        continue
  
                # If current character has odd frequency > 1 
                # take two characters which can be used in 
                # any of the parts 
                sumEven += 2
  
                # Update the frequency
                oddFreq[i] = oddFreq[i] - 2
  
        # If not possible, then every character of the 
        # string will act as a separate palindrome 
        return n
  
# Driver code
if __name__ == "__main__" :
  
    s = "noonpeep"
  
    print(countMinParts(s))
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
    // Function to return the frequency array
    // for the given string
    static int[] getFrequencies(String str)
    {
        int []freq = new int[26];
        for (int i = 0; i < str.Length; i++) 
        {
            freq[str[i] - 'a']++;
        }
        return freq;
    }
  
    // Function to return the required count
    static int countMinParts(String str)
    {
  
        int n = str.Length;
        int []freq = getFrequencies(str);
        List oddFreq = new List();
        List evenFreq = new List();
  
        int i, sumEven = 0;
  
        for (i = 0; i < 26; i++)
        {
            if (freq[i] == 0)
                continue;
  
            // Add frequencies of the even appearing 
            // characters
            if (freq[i] % 2 == 0)
                evenFreq.Add(freq[i]);
  
            // Count of the characters that appeared 
            // odd number of times
            else
                oddFreq.Add(freq[i]);
        }
  
        for (i = 0; i < evenFreq.Count; i++)
        {
            sumEven += evenFreq[i];
        }
  
        // If there are no characters with odd frequency
        if (oddFreq.Count == 0)
            return 1;
  
        // If there are no characters with even frequency
        if (sumEven == 0)
        {
  
            // Only a single character with odd frequency
            if (oddFreq.Count == 1)
                return 1;
  
            // More than 1 character with odd frequency
            // string isn't a palindrome
            return 0;
        }
  
        i = 0;
  
        // All odd appearing characters can also contribute to
        // the even length palindrome if one character
        // is removed from the frequency leaving it as even
        while (i < oddFreq.Count)
        {
  
            // If k palindromes are possible where k
            // is the number of characters with odd frequency
            if ((sumEven / 2) % oddFreq.Count == 0)
                return oddFreq.Count;
  
            // Current character can no longer be an element
            // in a string other than the mid character
            if (oddFreq[i] == 1) 
            {
                i++;
                continue;
            }
  
            // If current character has odd frequency > 1
            // take two characters which can be used in
            // any of the parts
            sumEven += 2;
  
            // Update the frequency
            oddFreq.Insert(i, oddFreq[i] - 2);
        }
  
        // If not possible, then every character of the 
        // string will act as a separate palindrome
        return n;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
  
        String s = "noonpeep";
  
        Console.WriteLine(countMinParts(s));
    }
}
  
// This code has been contributed by 29AjayKumar


输出:
1

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。