Find two unique Palindrome Strings using given String characters


In this problem, we will construct two palindromic strings using the given string’s characters.

We can use the character’s frequency to solve the problem. We can construct two new palindromic strings only if both characters’ frequencies are even or if any characters have an even frequency and others have an odd frequency.

Problem statement − We have given a string alpha containing two different characters and a size equal to N. We need to construct two palindromic strings using the characters of the alpha, which are not the same as the given string alpha.

Sample Examples

After incrementing each character of each prefix of a given size, the resultant string is ‘gffe’.

Input

alpha = "aaabbabbabb"

Output

bbbaaaaabbb, aabbbabbbaa

Explanation

The ‘bbbaaaaabbb’ and ‘aabbbabbbaa’ are different palindromic strings which we have constructed from the given string.

Input 

alpha = "aabbb"

Output 

abbba, babab

Input 

alpha = "aaabbabbabb"

Output 

bbbaaaaabbb, aabbbabbbaa

Explanation

Both output strings are new palindromic strings constructed from a given character’s string.

Input 

alpha = "aaabbb";

Output 

‘Not possible.’

Explanation

It is not possible to construct two different palindromic strings from the given string.

Approach 1

If the frequency of both characters is odd, it is not possible to construct two new palindromic strings. For example, in the ‘aaabbb’ string has 3 occurrences of ‘a’ and ‘b’. So, we can’t construct any single palindromic string.

If any single character’s frequency is even, we can always construct two different palindromic strings.

  • For even-odd character frequency: From the ‘aabbb’, we can construct the ‘abbba’ and ‘babab’ strings.

  • For even-even frequency of character: From the ‘aabb’, we can construct ‘abba’, and ‘baab’ types of string.

Algorithm

  • Step 1 − Define the ‘freq’ map to store the frequency of both characters and traverse the string to calculate the frequency of each character.

  • Step 2 − Define the ‘temp1’ and ‘temp2’ store two characters, and ‘freq1’ and ‘freq2’ variables to store the frequency of each character.

  • Step 3 − Traverse the map, and if flag == 1, assign the key to ‘temp1’ and value to the ‘freq1’. Also, initialize the ‘temp2’ and ‘freq2’ characters.

  • Step 4 − If ‘freq1’ and ‘freq2’ both are 1 or odd, print ‘not possible’, as we can’t construct two palindromic strings using the string characters.

  • Step 5 − If freq1 and freq2 are even, follow the steps below.

  • Step 5.1 − We need to print the first palindromic string. So, print the temp1 character for ‘freq1/2’ times, the ‘temp2’ character for the ‘freq2’ times, and print the temp1 character ‘freq1/2’ times again.

  • Step 5.2 − For the second string, print the temp2 character for ‘freq2/2’ times, the ‘temp1’ character for the ‘freq1’ times, and again print the temp2 character ‘freq2/2’ times.

  • Step 6 − If any one of freq1 and freq2 is odd, follow the steps below.

  • Step 6.1 − For the first string, if freq1 is even, print temp1 for freq1/2 times, temp2 for freq2 times, and temp1 for freq2/2 times. Otherwise, if freq2 is even, print temp2 for freq2/2 times, temp1 for freq1 times, and temp2 for freq1/2 times

  • Step 6.2 − For the second string, if freq1 is even, print temp2 for freq2/2 times, temp1 for freq1/2 times, single temp2 character to put in the middle of the string, freq1/2 temp1 character, and freq2/2 temp2 characters.

  • Step 6.3 − Otherwise, if freq1 is odd, print temp1 for freq2/2 times, temp2 for freq2/2 times, single temp1 character to put in the middle of the string, freq2/2 temp2 character, and freq1/2 temp1 characters.

Example

Following are the programs to the above algorithm −

#include <stdio.h>
#include <string.h>

// Function to find and print two palindrome strings
void find2Palindromes(const char* alpha) {
   // To store the frequency of characters
   int freq[256] = {0};

   // Calculating the frequency of each character
   for (int p = 0; alpha[p] != '\0'; p++) {
      freq[(int)alpha[p]] += 1;
   }

   char temp1 = ' ', temp2 = ' ';
   int freq1 = 0, freq2 = 0;
   int flag = 1;

   // Traverse the frequency array
   for (int i = 0; i < 256; i++) {
      if (freq[i] > 0) {
         // Get the frequency of the first character
         if (flag == 1) {
            temp1 = (char)i;
            freq1 = freq[i];
            flag++;
         }
         // Get the frequency of the second character
         else {
            temp2 = (char)i;
            freq2 = freq[i];
         }
      }
   }

   // Check whether two palindrome strings are possible
   if ((freq1 == 1 || freq2 == 1) || (freq1 % 2 == 1 && freq2 % 2 == 1)) {
      printf("not possible\n");
   }
   // Case 1 - Both are even
   else if (freq1 % 2 == 0 && freq2 % 2 == 0) {
      // Print half temp1
      for (int p = 1; p <= freq1 / 2; p++)
         printf("%c", temp1);

      // Print temp2
      for (int p = 1; p <= freq2; p++)
         printf("%c", temp2);

      // Print half temp1
      for (int p = 1; p <= freq1 / 2; p++)
         printf("%c", temp1);
      printf(" ");

      // Second palindrome string
      for (int p = 1; p <= freq2 / 2; p++)
         printf("%c", temp2);
      for (int p = 1; p <= freq1; p++)
         printf("%c", temp1);
      for (int p = 1; p <= freq2 / 2; p++)
         printf("%c", temp2);
   }
   // Case 2 - One is even, and one is odd
   else if (freq1 % 2 != 0 || freq2 % 2 != 0) {
      // Print the first string
      if (freq1 % 2 == 0) {
         for (int p = 1; p <= freq1 / 2; p++)
            printf("%c", temp1);
         for (int p = 1; p <= freq2; p++)
            printf("%c", temp2);
         for (int p = 1; p <= freq1 / 2; p++)
            printf("%c", temp1);
         printf(" ");
      } else {
         for (int p = 1; p <= freq2 / 2; p++)
            printf("%c", temp2);
         for (int p = 1; p <= freq1; p++)
            printf("%c", temp1);
         for (int p = 1; p <= freq2 / 2; p++)
            printf("%c", temp2);
         printf(" ");
      }

      // Print the second string
      if (freq1 % 2 == 0) {
         for (int p = 1; p <= freq2 / 2; p++)
            printf("%c", temp2);
         for (int p = 1; p <= freq1 / 2; p++)
            printf("%c", temp1);
         printf("%c", temp2);
         for (int p = 1; p <= freq1 / 2; p++)
            printf("%c", temp1);
         for (int p = 1; p <= freq2 / 2; p++)
            printf("%c", temp2);
      } else {
         for (int p = 1; p <= freq1 / 2; p++)
            printf("%c", temp1);
         for (int p = 1; p <= freq2 / 2; p++)
            printf("%c", temp2);
         printf("%c", temp1);
         for (int p = 1; p <= freq2 / 2; p++)
            printf("%c", temp2);
         for (int p = 1; p <= freq1 / 2; p++)
            printf("%c", temp1);
      }
   }
}

int main() {
   const char* alpha = "aaabbabbabb";
   printf("The original String is - %s\nPalindrome Strings are - ", alpha);
   find2Palindromes(alpha);
   return 0;
}

Output

The original String is - aaabbabbabb
Palindrome Strings are - bbbaaaaabbb aabbbabbbaa
#include <bits/stdc++.h>
using namespace std;
void find2Palindromes(string alpha) {
   // To store the frequency of characters
   map<char, int> freq;
   
   // Calculating the frequency of each character
   for (int p = 0; p < alpha.size(); p++) {
      freq[alpha[p]] += 1;
   }
   char temp1 = ' ', temp2 = ' ';
   int fre1 = 0, freq2 = 0;
   int flag = 1;
   
   // Traverse the map
   for (auto ch : freq) {
   
      // Get the frequency of the first character
      if (flag == 1) {
         temp1 = ch.first;
         fre1 = ch.second;
         flag++;
      }
      // Get the frequency of the second character
      else {
         temp2 = ch.first;
         freq2 = ch.second;
      }
   }
   // Check whether two palindrome strings are possible
   if ((fre1 == 1 || freq2 == 1) || (fre1 % 2 == 1) && (freq2 % 2 == 1)) {
      cout << "not possible";
      cout << endl;
   }
   
   // Case 1 - Both are even
   else if (fre1 % 2 == 0 && freq2 % 2 == 0) {
      // Print half temp1
      for (int p = 1; p <= fre1 / 2; p++)
      cout << temp1;
      
      // Print temp2
      for (int p = 1; p <= freq2; p++)
      cout << temp2;
      
      // Print half temp1
      for (int p = 1; p <= fre1 / 2; p++)
      cout << temp1;
      cout << " ";
      
      // Second palindrome string
      for (int p = 1; p <= freq2 / 2; p++)
         cout << temp2;
      for (int p = 1; p <= fre1; p++)
         cout << temp1;
      for (int p = 1; p <= freq2 / 2; p++)
         cout << temp2;
   }
   
   // Case 2 - One is even, and one is odd
   else if (fre1 % 2 != 0 || freq2 % 2 != 0) {
   
      // Print the first string
      if (fre1 % 2 == 0) {
         for (int p = 1; p <= fre1 / 2; p++)
            cout << temp1;
         for (int p = 1; p <= freq2; p++)
            cout << temp2;
         for (int p = 1; p <= fre1 / 2; p++)
            cout << temp1;
         cout << " ";
      } else {
         for (int p = 1; p <= freq2 / 2; p++)
            cout << temp2;
         for (int p = 1; p <= fre1; p++)
            cout << temp1;
         for (int p = 1; p <= freq2 / 2; p++)
            cout << temp2;
         cout << " ";
      }

      // Print the second string
      if (fre1 % 2 == 0) {
         for (int p = 1; p <= freq2 / 2; p++)
            cout << temp2;
         for (int p = 1; p <= fre1 / 2; p++)
            cout << temp1;
         cout << temp2;
         for (int p = 1; p <= fre1 / 2; p++)
            cout << temp1;
         for (int p = 1; p <= freq2 / 2; p++)
         cout << temp2;
      } else {
         for (int p = 1; p <= fre1 / 2; p++)
            cout << temp1;
         for (int p = 1; p <= freq2 / 2; p++)
            cout << temp2;
         cout << temp1;
         for (int p = 1; p <= freq2 / 2; p++)
            cout << temp2;
         for (int p = 1; p <= fre1 / 2; p++)
            cout << temp1;
      }
   }
}
int main() {
   string alpha = "aaabbabbabb";
   cout << "The original String is - " << alpha << endl << "Palindrome Strings are - ";
   find2Palindromes(alpha);
}

Output

The original String is - aaabbabbabb
Palindrome Strings are - bbbaaaaabbb aabbbabbbaa
import java.util.HashMap;
import java.util.Map;

public class PalindromeStrings {
   public static void find2Palindromes(String alpha) {
      // To store the frequency of characters
      Map<Character, Integer> freq = new HashMap<>();

      // Calculating the frequency of each character
      for (char c : alpha.toCharArray()) {
         freq.put(c, freq.getOrDefault(c, 0) + 1);
      }

      char temp1 = ' ', temp2 = ' ';
      int freq1 = 0, freq2 = 0;
      int flag = 1;

      // Traverse the map
      for (Map.Entry<Character, Integer> entry : freq.entrySet()) {
         // Get the frequency of the first character
         if (flag == 1) {
            temp1 = entry.getKey();
            freq1 = entry.getValue();
            flag++;
         }
         // Get the frequency of the second character
         else {
            temp2 = entry.getKey();
            freq2 = entry.getValue();
         }
      }

      // Check whether two palindrome strings are possible
      if ((freq1 == 1 || freq2 == 1) || (freq1 % 2 == 1 && freq2 % 2 == 1)) {
         System.out.println("not possible");
      }
      // Case 1 - Both are even
      else if (freq1 % 2 == 0 && freq2 % 2 == 0) {
         // Print half temp1
         for (int p = 1; p <= freq1 / 2; p++) {
            System.out.print(temp1);
         }
         // Print temp2
         for (int p = 1; p <= freq2; p++) {
            System.out.print(temp2);
         }
         // Print half temp1
         for (int p = 1; p <= freq1 / 2; p++) {
            System.out.print(temp1);
         }
         System.out.print(" ");

         // Second palindrome string
         for (int p = 1; p <= freq2 / 2; p++) {
            System.out.print(temp2);
         }
         for (int p = 1; p <= freq1; p++) {
            System.out.print(temp1);
         }
         for (int p = 1; p <= freq2 / 2; p++) {
             System.out.print(temp2);
         }
      }
      // Case 2 - One is even, and one is odd
      else {
         // Print the first string
         if (freq1 % 2 == 0) {
            for (int p = 1; p <= freq1 / 2; p++) {
               System.out.print(temp1);
            }
            for (int p = 1; p <= freq2; p++) {
               System.out.print(temp2);
            }
            for (int p = 1; p <= freq1 / 2; p++) {
               System.out.print(temp1);
            }
            System.out.print(" ");
         } else {
            for (int p = 1; p <= freq2 / 2; p++) {
               System.out.print(temp2);
            }
            for (int p = 1; p <= freq1; p++) {
               System.out.print(temp1);
            }
            for (int p = 1; p <= freq2 / 2; p++) {
               System.out.print(temp2);
            }
            System.out.print(" ");
         }

         // Print the second string
         if (freq1 % 2 == 0) {
            for (int p = 1; p <= freq2 / 2; p++) {
               System.out.print(temp2);
            }
            for (int p = 1; p <= freq1 / 2; p++) {
               System.out.print(temp1);
            }
            System.out.print(temp2);
            for (int p = 1; p <= freq1 / 2; p++) {
               System.out.print(temp1);
            }
            for (int p = 1; p <= freq2 / 2; p++) {
               System.out.print(temp2);
            }
         } else {
            for (int p = 1; p <= freq1 / 2; p++) {
               System.out.print(temp1);
            }
            for (int p = 1; p <= freq2 / 2; p++) {
               System.out.print(temp2);
            }
            System.out.print(temp1);
            for (int p = 1; p <= freq2 / 2; p++) {
               System.out.print(temp2);
            }
            for (int p = 1; p <= freq1 / 2; p++) {
               System.out.print(temp1);
            }
         }
      }
   }

   public static void main(String[] args) {
      String alpha = "aaabbabbabb";
      System.out.println("The original String is - " + alpha);
      System.out.print("Palindrome Strings are - ");
      find2Palindromes(alpha);
   }
}

Output

The original String is - aaabbabbabb
Palindrome Strings are - bbbaaaaabbb aabbbabbbaa
def find_2_palindromes(alpha):
   # To store the frequency of characters
   freq = {}

   # Calculating the frequency of each character
   for char in alpha:
      freq[char] = freq.get(char, 0) + 1

   temp1, temp2 = ' ', ' '
   freq1, freq2 = 0, 0
   flag = 1

   # Traverse the dictionary
   for char, count in freq.items():
      # Get the frequency of the first character
      if flag == 1:
         temp1 = char
         freq1 = count
         flag += 1
      # Get the frequency of the second character
      else:
         temp2 = char
         freq2 = count

   # Check whether two palindrome strings are possible
   if freq1 == 1 or freq2 == 1 or (freq1 % 2 == 1 and freq2 % 2 == 1):
      print("not possible")
   else:
      # Case 1 - Both are even
      if freq1 % 2 == 0 and freq2 % 2 == 0:
         # Print half temp1
         print(temp1 * (freq1 // 2), end='')

         # Print temp2
         print(temp2 * freq2, end='')

         # Print half temp1
         print(temp1 * (freq1 // 2), end=' ')
          
         # Second palindrome string
         print(temp2 * (freq2 // 2), end='')
         print(temp1 * freq1, end='')
         print(temp2 * (freq2 // 2))
      else:
         # Print the first string
         if freq1 % 2 == 0:
            print(temp1 * (freq1 // 2), end='')
            print(temp2 * freq2, end='')
            print(temp1 * (freq1 // 2), end=' ')
         else:
            print(temp2 * (freq2 // 2), end='')
            print(temp1 * freq1, end='')
            print(temp2 * (freq2 // 2), end=' ')
          
         # Print the second string
         if freq1 % 2 == 0:
            print(temp2 * (freq2 // 2), end='')
            print(temp1 * (freq1 // 2), end='')
            print(temp2, end='')
            print(temp1 * (freq1 // 2), end='')
            print(temp2 * (freq2 // 2))
         else:
            print(temp1 * (freq1 // 2), end='')
            print(temp2 * (freq2 // 2), end='')
            print(temp1, end='')
            print(temp2 * (freq2 // 2), end='')
            print(temp1 * (freq1 // 2))

# Main function
if __name__ == "__main__":
   alpha = "aaabbabbabb"
   print("The original String is -", alpha)
   print("Palindrome Strings are -", end=' ')
   find_2_palindromes(alpha)

Output

The original String is - aaabbabbabb
Palindrome Strings are - bbbaaaaabbb aabbbabbbaa

Time complexity – O(N) for traversing the string multiple times.

Space complexity – O(1) as we print palindromic strings without using the extra space.

We can create two different palindrome strings from the given strings by putting the first characters in the middle of the first string and the second character in the middle of the second string.

Programmers may replace the for loop with the substr() method to shorten the code. First, we can create a string using the String constructor containing the freq1 times' temp1 characters and freq2 times' temp2 characters. After that, we can extract the substring of a particular length from both strings whenever we require.

Updated on: 20-Oct-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements