Palindrome check for concatenation of Prefixes and Suffixes
Last Updated :
13 Sep, 2023
Given an integer N and an array of strings arr[], the task is to check whether the concatenation of prefixes and suffixes of all strings forms a palindrome or not.
Examples:
Input: N = 4, arr[] = {“bcd”, “cd”, “a”, “d”, “abc”, “ab”}
Output: No
Explanation: “a”, “ab” and “abc” are prefixes. “d”, “cd” and “bcd” are suffixes. So the final string is “abcd” which is not a palindrome.
and
Input: N = 3, arr[] = {“i”, “io”, “i”, “oi”}
Output: Yes
Explanation: “i” and “io” are prefixes. “i” and “oi” are suffixes. The final string is “ioi” which is a palindrome.
Approach: This can be solved with the following idea:
If we have a string s of length N and we know that there are exactly two strings of length N – 1 (one prefix and one suffix), which we will call x and y, then we can determine whether s is a palindrome by checking if the reversal of x is equal to y. In other words, if s is a palindrome, then the prefix x and suffix y must be the same string when read in reverse order. Conversely, if the prefix and suffix are the same when read in reverse order, then s is a palindrome.
Below are the steps involved in the implementation of the code:
- Define the palindrome function that takes two strings a and b as input and returns a Boolean value indicating whether the two strings can be concatenated to form a palindrome of length 2*n-2.
- Define the solve function that takes a vector of strings arr and an integer n as input.
- Use a loop to iterate through all strings in the input array arr[]. For each iteration, check whether the length of the string is n-1. If it is, add the string to the vector v.
- Call the palindrome function with the first two strings in the vector v. If the function returns true, print “YES” and return.
- If the function returns false, call the palindrome function with the second and first string in the vector v. If the function returns true, print “YES” and return.
- If both calls to the palindrome function return false, print “NO“.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool palindrome(string a, string b)
{
int n = a.size();
if (a[0] != b[n - 1])
return 0;
for ( int i = 1, j = n - 2; i < n, j >= 0; i++, j--) {
if (a[i] != b[j])
return 0;
}
return 1;
}
void solve(vector<string> arr, int n)
{
vector<string> v;
for ( int i = 0; i < 2 * n - 2; i++) {
if (arr[i].size() == n - 1)
v.push_back(arr[i]);
}
if (palindrome(v[0], v[1]) || palindrome(v[1], v[0]))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main()
{
int n = 6;
vector<string> arr
= { "ltw" , "ti" , "l" , "ltwp" , "i" ,
"lt" , "wpti" , "pti" , "ltwpt" , "twpti" };
solve(arr, n);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class GFG {
static boolean palindrome(String a, String b) {
int n = a.length();
if (a.charAt( 0 ) != b.charAt(n - 1 ))
return false ;
for ( int i = 1 , j = n - 2 ; i < n && j >= 0 ; i++, j--) {
if (a.charAt(i) != b.charAt(j))
return false ;
}
return true ;
}
static void solve(List<String> arr, int n) {
List<String> v = new ArrayList<>();
for ( int i = 0 ; i < 2 * n - 2 ; i++) {
if (arr.get(i).length() == n - 1 ) {
v.add(arr.get(i));
}
}
if (palindrome(v.get( 0 ), v.get( 1 )) || palindrome(v.get( 1 ), v.get( 0 ))) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
public static void main(String[] args) {
int n = 6 ;
List<String> arr = List.of( "ltw" , "ti" , "l" , "ltwp" , "i" , "lt" , "wpti" , "pti" , "ltwpt" , "twpti" );
solve(arr, n);
}
}
|
Python3
def palindrome(a, b):
n = len (a)
if a[ 0 ] ! = b[n - 1 ]:
return False
for i in range ( 1 , n):
j = n - i - 1
if a[i] ! = b[j]:
return False
return True
def solve(arr, n):
v = []
for i in range ( 2 * n - 2 ):
if len (arr[i]) = = n - 1 :
v.append(arr[i])
if palindrome(v[ 0 ], v[ 1 ]) or palindrome(v[ 1 ], v[ 0 ]):
print ( "YES" )
else :
print ( "NO" )
n = 6
arr = [ "ltw" , "ti" , "l" , "ltwp" , "i" , "lt" , "wpti" , "pti" , "ltwpt" , "twpti" ]
solve(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool Palindrome( string a, string b)
{
int n = a.Length;
if (a[0] != b[n - 1])
return false ;
for ( int i = 1, j = n - 2; i < n && j >= 0; i++, j--)
{
if (a[i] != b[j])
return false ;
}
return true ;
}
static void Solve(List< string > arr, int n)
{
List< string > v = new List< string >();
for ( int i = 0; i < 2 * n - 2; i++)
{
if (arr[i].Length == n - 1)
{
v.Add(arr[i]);
}
}
if (Palindrome(v[0], v[1]) || Palindrome(v[1], v[0]))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
static void Main( string [] args)
{
int n = 6;
List< string > arr = new List< string > { "ltw" , "ti" , "l" , "ltwp" , "i" , "lt" , "wpti" , "pti" , "ltwpt" , "twpti" };
Solve(arr, n);
}
}
|
Javascript
function palindrome(a, b) {
const n = a.length;
if (a[0] !== b[n - 1])
return false ;
for (let i = 1, j = n - 2; i < n && j >= 0; i++, j--) {
if (a[i] !== b[j])
return false ;
}
return true ;
}
function solve(arr, n) {
const v = [];
for (let i = 0; i < 2 * n - 2; i++) {
if (arr[i].length === n - 1)
v.push(arr[i]);
}
if (palindrome(v[0], v[1]) || palindrome(v[1], v[0]))
console.log( "YES" );
else
console.log( "NO" );
}
const n = 6;
const arr = [
"ltw" , "ti" , "l" , "ltwp" , "i" ,
"lt" , "wpti" , "pti" , "ltwpt" , "twpti"
];
solve(arr, n);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...