Home > Backend Development > C++ > body text

Checks whether the substrings of three given strings can be concatenated into a palindrome string

WBOY
Release: 2023-08-30 17:05:06
forward
622 people have browsed it

Checks whether the substrings of three given strings can be concatenated into a palindrome string

Palindromes are a fascinating topic in computer science and programming. A palindrome is a sequence of words, phrases, numbers, or other characters that is read the same from front to back as from back to front, ignoring spaces, punctuation, and case. In this article, we will examine a unique problem: how to determine whether substrings from three given strings can be concatenated to form a palindrome. This question is a common interview question and can be solved using a variety of techniques, including string manipulation, hashing, and dynamic programming.

Problem Statement

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

method

The general approach to solving this problem involves the following steps -

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

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

To check whether a string can form a palindrome, we need to ensure that no more than one character appears with odd frequency in the string.

C Solution

Example

This is the C function that implements the above method −

#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;
}
Copy after login

Output

No
Copy after login

Example test case description

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

Function canFormPalindrome(str) checks whether the entire string can be rearranged into a palindrome, instead of checking whether it is already a palindrome.

Using the strings "abc", "de" and "edcba", the string "abcdeedcba" obtained by concatenating them cannot be rearranged into a palindrome because there are two 'd' characters and two 'e's in it. ' characters, but only one 'b' character. Therefore, the output is indeed "No".

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

in conclusion

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

The above is the detailed content of Checks whether the substrings of three given strings can be concatenated into a palindrome string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template