Partition a string into palindromic strings of at least length 2 with every character present in a single string


Partitioning a string into palindromic strings of at least length 2 with every character present in a single string is a challenging problem in computer science. The task is to take a string and divide it into multiple substrings, each consisting of at least two characters and containing every character of the original string only once. The objective is to determine if each substring is a palindrome or not.

In this tutorial, we will provide a solution to this problem using C++. We will discuss the algorithm and the code implementation step by step, along with providing two test examples to help you understand the concept better. By the end of this tutorial, you will have a clear understanding of how to partition a string into palindromic substrings of at least length 2 with every character present in a single substring. So, let's dive in and explore this interesting problem in detail.

Problem Statement

You are given a string 'S' consisting of 'N' lowercase alphabets. Your task is to determine if there exist any palindromic strings of length greater than or equal to 2 that can be formed by selecting every character of the string 'S' exactly once. If such strings exist, print "Yes". Otherwise, print "No".

Sample Examples

Example 1

Input: S = "abbccdd"
Output: Yes

Explanation: The following palindromic strings can be formed by selecting every character of S exactly once - "abba", "cc", "dd". Hence, the output is "Yes".

Example 2

Input: ""abc""
Output: No

Explanation: The only possible strings that can be formed by selecting every character of S exactly once are "ab" and "ac", neither of which is palindromic. Hence, the output is "No".

Algorithm

STEP 1: Initialize an array 'a' of size 26 with all elements 0 to store the frequency of each character.

STEP 2: Initialize two variables 'o' and 'e' to store the frequency of characters with frequencies 1 and even respectively to 0.

STEP 3: Traverse the string 'S' and update the frequency of each character in the 'a' array.

STEP 4: Iterate over all the characters of the 'a' array.

STEP 5: If the frequency of the character is 1, increment 'o'.

STEP 6: If the frequency of the character is even and greater than 0, increment 'e' by half of the frequency.

STEP 7: If the value of 'e' is greater than or equal to 'o', print "Yes" and return.

STEP 8. Otherwise, calculate the number of characters with a frequency equal to 1 that are not part of a palindromic string and store it in 'o'.

STEP 9: Iterate over all the characters of the 'a' array.

STEP 10: If the value of 'o' becomes less than or equal to 0, break out of the loop.

STEP 11: If the frequency of the current character is odd, greater than 2, and 'o' is greater than 0, then subtract half of the frequency from 'o'.

STEP 12: If a single character is still remaining and 'o' is greater than 0, increment 'o' by 1 and set the frequency of the current character to 1.

STEP 13: If the value of 'o' is less than or equal to 0, print "Yes". Otherwise, print "No".

The algorithm takes O(N) time complexity, where N is the length of the string 'S'. Now, we are going to understand the implementation of the above algorithm using C++ with an example. So let’s do it!

Example

Implementation of the above algorithm using C++

The below program checks if a given string can be partitioned into palindromic substrings of at least length 2, with every character present in a single string. The program uses an array to store the frequency of each character in the string and then checks if the frequency of all characters allows for a valid partition. If the partition is possible, the program outputs "Yes", otherwise it outputs "No".

#include <iostream>
using namespace std;
void checkPalindrome(string& s){
    int a[26] = { 0 };
    int o = 0, e = 0;    
    for (int i = 0; s[i] != '\0'; i++)
        a[(int)s[i] - 97]++;    
    for (int i = 0; i < 26; i++) {
        if (a[i] == 1)
            o++;
        else if (a[i] % 2 == 0 and a[i] != 0)
            e += (a[i] / 2);
    }    
    if (e >= o)
        cout << "Yes" << endl;
    else {
        o = o - e;
        for (int i = 0; i < 26; i++) {
            if (o <= 0)
                break;
            if (o > 0 and a[i] % 2 == 1 and a[i] > 2) {
                int k = o;
                o = o - a[i] / 2;
                if (o > 0 or 2 * k + 1 == a[i]) {
                    o++;
                    a[i] = 1;
                }
            }
        }
        if (o <= 0)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}
int main(){
    string str1 = "racecar";
    string str2 = "abc";    
    cout << "Input 1: " << str1 << endl;
    cout << "Output 1: ";
    checkPalindrome(str1);    
    cout << "Input 2: " << str2 << endl;
    cout << "Output 2: ";
    checkPalindrome(str2);
    return 0;
}

Output

Input 1: racecar
Output 1: Yes
Input 2: abc
Output 2: No

Conclusion

To sum up, partitioning a string into palindromic strings of at least length 2 with every character present in a single string can be achieved efficiently using the approach discussed in this tutorial. By carefully analyzing the problem statement and utilizing the frequency of characters, we can determine if the given string can be partitioned or not. The provided C++ program demonstrates the implementation of this approach and can be used as a starting point for solving similar problems.

Updated on: 08-Sep-2023

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements