Check if concatenation of splitted substrings of two given strings forms a palindrome or not
Last Updated :
08 Feb, 2022
Given two strings a and b of the same length, the task is to check if splitting both the strings and concatenating their opposite substrings, i.e. concatenating the left substring of a with right substring of b or concatenating the left substring of b with right substring of a, forms a palindrome or not. If found to be true print "Yes". Otherwise, print "No".
Note: One of the splitted substrings can be empty.
Examples:
Input: a = "x", b = "y"
Output: Yes
Explanation:
Split both the strings at index 0.
Left substring of a (aLeft) = " ", Right substring of a (aRight) = "x"
Left substring of b (bLeft) = " ", Right substring of b (bRight) = "y"
Since aLeft + bRight = " " + "y" = "y", which is a palindrome as well as bLeft + aRight= " " + "x" = "x" is also a palindrome, print Yes.
Input: a = "ulacfd", b = "jizalu"
Output: True
Explanation:
Split both the strings at index 3:
Left substring of a (aLeft) = "ula", Right substring of a (aRight) = "cfd"
, Left substring of b (bLeft) = "jiz", Right substring of b (bRight) = "alu"
Since aleft + bright = "ula" + "alu" = "ulaalu", which is a palindrome, print Yes.
Approach: The idea is to use Two Pointer technique to solve this problem. Follow the steps below to solve the problem:
- Place a pointer i at 0th index of a and "j" at the last index of b.
- Iterate over the characters of the string and check if a[i] == b[j], then increment i and decrement j.
- Otherwise, just break the loop as its not a palindrome type sequence.
- Concatenate aLeft and bRight in a string variable xa and aRight and bLeft in another string variable xb.
- Check if either of the two strings is a palindrome or not. If found to be true, print "Yes". Otherwise, print "No".
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if concatenating
// opposite substrings after splitting
// two given strings forms a palindrome
// or not
bool checkSplitting(string a, string b)
{
// Length of the string
int len = a.length();
int i = 0, j = len - 1;
// Iterate through the strings
while (i < len)
{
// If not a palindrome sequence
if (a[i] != b[j])
{
break;
}
i += 1;
j -= 1;
// Concatenate left substring of a
// and right substring of b in xa
// Concatenate right substring of a
// and left substring of b in xb
string xa = a.substr(i, j + 1);
string xb = b.substr(i, j + 1);
// Check if either of the two concatenated
// strings is a palindrome or not
if (xa == string(xa.rbegin(), xa.rend()) ||
xb == string(xb.rbegin(), xb.rend()))
return true;
}
}
// Function to check if concatenation of splitted
// substrings of two given strings forms a palindrome
void isSplitPossible(string a, string b)
{
if (checkSplitting(a, b) == true)
{
cout << "Yes";
}
else if (checkSplitting(b, a) == true)
{
cout << "Yes";
}
else
{
cout << "No";
}
}
// Driver Code
int main()
{
string a = "ulacfd", b = "jizalu";
// Function Call
isSplitPossible(a, b);
return 0;
}
// This code is contributed by pushpendrayadav1057
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if concatenating
// opposite subStrings after splitting
// two given Strings forms a palindrome
// or not
static boolean checkSplitting(String a, String b)
{
// Length of the String
int len = a.length();
int i = 0, j = len - 1;
// Iterate through the Strings
while (i < len)
{
// If not a palindrome sequence
if (a.charAt(i) != b.charAt(j))
{
break;
}
i += 1;
j -= 1;
// Concatenate left subString of a
// and right subString of b in xa
// Concatenate right subString of a
// and left subString of b in xb
String xa = a.substring(i, j + 1);
String xb = b.substring(i, j + 1);
// Check if either of the two concatenated
// Strings is a palindrome or not
if (xa.equals(reverse(xa))||xb.equals(reverse(xb)))
return true;
}
return false;
}
// Function to check if concatenation of splitted
// subStrings of two given Strings forms a palindrome
static void isSplitPossible(String a, String b)
{
if (checkSplitting(a, b) == true)
{
System.out.print("Yes");
}
else if (checkSplitting(b, a) == true)
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
static String reverse(String input) {
char[] a = input.toCharArray();
int l, r = a.length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
// Driver Code
public static void main(String[] args)
{
String a = "ulacfd", b = "jizalu";
// Function Call
isSplitPossible(a, b);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to check if concatenating
# opposite substrings after splitting
# two given strings forms a palindrome or not
def checkSplitting(a, b, n):
i, j = 0, n - 1
# Iterate through the strings
while(i < n):
# If not a palindrome sequence
if(a[i] != b[j]):
break
i += 1
j -= 1
# Concatenate left substring of a
# and right substring of b in xa
# Concatenate right substring of a
# and left substring of b in xb
xa = a[i:j + 1]
xb = b[i:j + 1]
# Check if either of the two concatenated
# strings is a palindrome or not
if(xa == xa[::-1] or xb == xb[::-1]):
return True
# Function to check if concatenation of splitted
# substrings of two given strings forms a palindrome
def isSplitPossible(a, b):
if checkSplitting(a, b, len(a)) == True:
print("Yes")
elif checkSplitting(b, a, len(a)) == True:
print("Yes")
else:
print("No")
# Given string a and b
a = "ulacfd"
b = "jizalu"
# Function Call
isSplitPossible(a, b)
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if concatenating
// opposite subStrings after splitting
// two given Strings forms a palindrome
// or not
static bool checkSplitting(String a, String b)
{
// Length of the String
int len = a.Length;
int i = 0, j = len - 1;
// Iterate through the Strings
while (i < len)
{
// If not a palindrome sequence
if (a[i] != b[j])
{
break;
}
i += 1;
j -= 1;
// Concatenate left subString of a
// and right subString of b in xa
// Concatenate right subString of a
// and left subString of b in xb
String xa = a.Substring(i, j + 1 - i);
String xb = b.Substring(i, j + 1 - i);
// Check if either of the two concatenated
// Strings is a palindrome or not
if (xa.Equals(reverse(xa)) ||
xb.Equals(reverse(xb)))
return true;
}
return false;
}
// Function to check if concatenation of splitted
// subStrings of two given Strings forms a palindrome
static void isSplitPossible(String a, String b)
{
if (checkSplitting(a, b) == true)
{
Console.Write("Yes");
}
else if (checkSplitting(b, a) == true)
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
static String reverse(String input)
{
char[] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join("",a);
}
// Driver Code
public static void Main(String[] args)
{
String a = "ulacfd", b = "jizalu";
// Function Call
isSplitPossible(a, b);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program for the above approach
// Function to check if the string is palindrome or not
function checkPalindrome(str) {
// find the length of a string
var len = str.length;
// loop through half of the string
for (var i = 0; i < parseInt(len / 2); i++) {
// check if first and last string are same
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
// Function to check if concatenating
// opposite substrings after splitting
// two given strings forms a palindrome
// or not
function checkSplitting(a, b)
{
// Length of the string
var len = a.length;
var i = 0, j = len - 1;
// Iterate through the strings
while (i < len)
{
// If not a palindrome sequence
if (a[i] != b[j])
{
break;
}
i += 1;
j -= 1;
// Concatenate left substring of a
// and right substring of b in xa
// Concatenate right substring of a
// and left substring of b in xb
var xa = a.substring(i, j + 1);
var xb = b.substring(i, j + 1);
// Check if either of the two concatenated
// strings is a palindrome or not
if (checkPalindrome(xa)==true || checkPalindrome(xb)==true)
return true;
}
}
// Function to check if concatenation of splitted
// substrings of two given strings forms a palindrome
function isSplitPossible(a, b)
{
if (checkSplitting(a, b) == true)
{
document.write( "Yes");
}
else if (checkSplitting(b, a) == true)
{
document.write("Yes");
}
else
{
document.write( "No");
}
}
var a = "ulacfd", b = "jizalu";
// Function Call
isSplitPossible(a, b);
// This code is contributed by SoumikMondal
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings Given two strings A and B of length N, the task is to check if any of the two strings formed by splitting both the strings at any index i (0 ⤠i ⤠N - 1) and concatenating A[0, i] and B[i, N - 1] or A[i, N - 1] and B[0, i] respectively, form a palindromic string or not. If found to be true, print "Y
15+ min read
Count pair of strings whose concatenation of substrings form a palindrome Given an array of strings arr[], the task is to count the pair of strings whose concatenation of substrings form a palindrome.Examples: Input: arr[] = {"gfg", "gfg"} Output: 1 Explanation: One possible way of choosing s1 and s2 is s1 = "gf", s2 = "g" such that s1 + s2 i.e "gfg" is a palindrome.Input
5 min read
Count of pairs of strings whose concatenation forms a palindromic string Given an array A[ ] consisting of N strings, the task is to count the number of pairs of possible strings that on merging forms a Palindromic String or can be rearranged to form a Palindromic String. Example : Input: N = 6, A[ ] = {aab, abcac, dffe, ed, aa, aade}Output: 6Explanation: All possible pa
9 min read
Check if a string contains a palindromic sub-string of even length S is string containing only lowercase English alphabets. We need to find if there exists at least one palindromic sub-string whose length is even. Examples: Input : aassssOutput : YESInput : gfgOutput : NOApproach: Approach to solve this problem is to check all even-length substrings of the given st
8 min read
Rearrange characters of a string to make it a concatenation of palindromic substrings Given a string S consisting of lowercase alphabets, the task is to check whether the given string can be rearranged such that the string can be split into non-overlapping palindromic substrings of at least length 2. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "
6 min read
Count of three non-overlapping sub-strings which on concatenation forms a palindrome Given a string str, the task is to count the number of ways a palindromic substring could be formed by the concatenation of three sub-strings x, y and z of the string str such that all of them are non-overlapping i.e. sub-string y occurs after substring x and sub-string z occurs after sub-string y.E
7 min read
Check if it is possible to create a palindrome string from given N Given a number N. The task is to create an alphabetical string in lower case from that number and tell whether the string is palindrome or not. a = 0, b = 1â¦.. and so on. For eg: If the number is 61 the substring âgbâ will be printed till 7 (6+1) characters i.e. âgbgbgbgâ and check if palindrome or
7 min read
Longest palindromic string formed by concatenation of prefix and suffix of a string Given string str, the task is to find the longest palindromic substring formed by the concatenation of the prefix and suffix of the given string str. Examples: Input: str = "rombobinnimor" Output: rominnimor Explanation: The concatenation of string "rombob"(prefix) and "mor"(suffix) is "rombobmor" w
10 min read
Longest palindromic string possible by concatenating strings from a given array Given an array of strings S[] consisting of N distinct strings of length M. The task is to generate the longest possible palindromic string by concatenating some strings from the given array. Examples: Input: N = 4, M = 3, S[] = {"omg", "bbb", "ffd", "gmo"}Output: omgbbbgmoExplanation: Strings "omg"
8 min read
Count pairs of non overlapping Substrings of size K which form a palindromic String Given a string S of length l, the task is to count the number of pairs of two non-overlapping substrings of length K of the string S, such that on joining the two non-overlapping substrings the resultant string is a palindrome. Examples: Input: S = "abcdbadc", K = 2Output: 2 Explanation: Possible su
15 min read