Check if substrings from three given strings can be concatenated to form a palindrome


Palindromes are a fascinating topic in computer science and programming. A palindrome is a word, phrase, number, or other sequences of characters that read the same forward and backward, ignoring spaces, punctuation, and capitalization. In this article, we will investigate a unique problem: how to determine if substrings from three given strings can be concatenated to form a palindrome. This problem is a common interview question and can be solved using various techniques, including string manipulation, hashing, and dynamic programming.

Problem Statement

Given three strings, the task is to check if it's possible to select substrings from each of the given strings and concatenate them to form a palindrome.

Approach

The general approach to solve this problem includes the following steps −

  • Concatenate the three strings in six different ways (all permutations of the three strings).

  • For each concatenated string, check if it can form a palindrome.

To check if a string can form a palindrome, we need to ensure that no more than one character has an odd frequency in the string.

C++ Solution

Example

Here's the C++ function that implements the above approach −

#include<bits/stdc++.h>
using namespace std;

bool canFormPalindrome(string str) {
   vector<int> count(256, 0);
   for (int i = 0; str[i]; i++)
      count[str[i]]++;
   int odd = 0;
   for (int i = 0; i < 256; i++) {
      if (count[i] & 1)
         odd++;
      if (odd > 1)
         return false;
   }
   return true;
}

bool checkSubstrings(string s1, string s2, string s3) {
   string arr[] = {s1, s2, s3, s1, s3, s2};
   for (int i = 0; i < 3; i++) {
      if (canFormPalindrome(arr[i] + arr[i + 1] + arr[i + 2]))
         return true;
   }
   return false;
}

int main() {
   string s1 = "abc";
   string s2 = "def";
   string s3 = "cba";
   if (checkSubstrings(s1, s2, s3))
      cout << "Yes\n";
   else
      cout << "No\n";
   return 0;
}

Output

No

Example Test Case Explanation

Let's take the strings as "abc", "def", and "cba".

The function canFormPalindrome(str) checks if the entire string can be rearranged to form a palindrome, not whether it is already a palindrome.

With the strings "abc", "de", and "edcba", the concatenation "abcdeedcba" cannot be rearranged to form a palindrome because there are two 'd' characters and two 'e' characters, but there is only one 'b' character. Therefore, the output is indeed "No".

The function checkSubstrings checks all possible concatenations of the three strings. However, none of these can be rearranged to form a palindrome, so the output is "No".

Conclusion

Being able to solve such problems not only helps in acing coding interviews but also enhances problem-solving skills, which are essential for every software engineer. This problem is a good example of how to use string manipulation and hashing to solve complex problems. Practice and understanding are key to mastering these topics.

Updated on: 17-May-2023

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements