Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
Last Updated :
15 Dec, 2022
Given two strings A and B of equal lengths, the task is to find an index i such that A[0…i] and B[i+1…n-1] give a palindrome when concatenated together. If it is not possible to find such an index then print -1.
Examples:
Input: S1 = “abcdf”, S2 = “sfgba”
Output: 1
S1[0..1] = “ab”, S2[2..n-1] = “gba”
S1 + S2 = “abgba” which is a palindrome.
Input : S1 = “abcda”, S2 = “bacbs”
Output: -1
Simple Approach:
- Iterate from 0 to n (length of the string) and copy ith character from S1 to another string let’s say S.
- Now take another temporary string Temp and copy the characters of S2 from index i +1 to n.
- Now check whether the string (S + Temp) is palindrome or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string s)
{
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s[i] != s[j])
return false ;
i++;
j--;
}
return true ;
}
int getIndex(string S1, string S2, int n)
{
string S = "" ;
for ( int i = 0; i < n; i++) {
S = S + S1[i];
string Temp = "" ;
for ( int j = i + 1; j < n; j++)
Temp += S2[j];
if (isPalindrome(S + Temp)) {
return i;
}
}
return -1;
}
int main()
{
string S1 = "abcdf" , S2 = "sfgba" ;
int n = S1.length();
cout << getIndex(S1, S2, n);
return 0;
}
|
Java
class GFG
{
static boolean isPalindrome(String s)
{
int i = 0 ;
int j = s.length() - 1 ;
while (i < j)
{
if (s.charAt(i) != s.charAt(j))
return false ;
i++;
j--;
}
return true ;
}
static int getIndex(String S1, String S2, int n)
{
String S = "" ;
for ( int i = 0 ; i < n; i++)
{
S = S + S1.charAt(i);
String Temp = "" ;
for ( int j = i + 1 ; j < n; j++)
Temp += S2.charAt(j);
if (isPalindrome(S + Temp))
{
return i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
String S1 = "abcdf" , S2 = "sfgba" ;
int n = S1.length();
System.out.println(getIndex(S1, S2, n));
}
}
|
Python3
def isPalindrome(s):
i = 0 ;
j = len (s) - 1 ;
while (i < j):
if (s[i] is not s[j]):
return False ;
i + = 1 ;
j - = 1 ;
return True ;
def getIndex(S1, S2, n):
S = "";
for i in range (n):
S = S + S1[i];
Temp = "";
for j in range (i + 1 , n):
Temp + = S2[j];
if (isPalindrome(S + Temp)):
return i;
return - 1 ;
S1 = "abcdf" ; S2 = "sfgba" ;
n = len (S1);
print (getIndex(S1, S2, n));
|
C#
using System;
class GFG
{
static bool isPalindrome( string s)
{
int i = 0;
int j = s.Length - 1;
while (i < j)
{
if (s[i] != s[j])
return false ;
i++;
j--;
}
return true ;
}
static int getIndex( string S1,
string S2, int n)
{
string S = "" ;
for ( int i = 0; i < n; i++)
{
S = S + S1[i];
string Temp = "" ;
for ( int j = i + 1; j < n; j++)
Temp += S2[j];
if (isPalindrome(S + Temp))
{
return i;
}
}
return -1;
}
public static void Main()
{
string S1 = "abcdf" , S2 = "sfgba" ;
int n = S1.Length;
Console.WriteLine(getIndex(S1, S2, n));
}
}
|
PHP
<?php
function isPalindrome( $s )
{
$i = 0;
$j = strlen ( $s ) - 1;
while ( $i < $j )
{
if ( $s [ $i ] != $s [ $j ])
return false;
$i ++;
$j --;
}
return true;
}
function getIndex( $S1 , $S2 , $n )
{
$S = "" ;
for ( $i = 0; $i < $n ; $i ++)
{
$S = $S . $S1 [ $i ];
$Temp = "" ;
for ( $j = $i + 1; $j < $n ; $j ++)
$Temp .= $S2 [ $j ];
if (isPalindrome( $S . $Temp ))
{
return $i ;
}
}
return -1;
}
$S1 = "abcdf" ; $S2 = "sfgba" ;
$n = strlen ( $S1 );
echo getIndex( $S1 , $S2 , $n );
?>
|
Javascript
<script>
function isPalindrome(s)
{
let i = 0;
let j = s.length - 1;
while (i < j)
{
if (s[i] != s[j])
return false ;
i++;
j--;
}
return true ;
}
function getIndex(S1, S2, n)
{
let S = "" ;
for (let i = 0; i < n; i++)
{
S = S + S1[i];
let Temp = "" ;
for (let j = i + 1; j < n; j++)
Temp += S2[j];
if (isPalindrome(S + Temp))
{
return i;
}
}
return -1;
}
let S1 = "abcdf" , S2 = "sfgba" ;
let n = S1.length;
document.write(getIndex(S1, S2, n));
</script>
|
Time complexity: O(n2), where n is the length of the given strings.
Auxiliary Space: O(n)
Efficient Approach: The efficient approach will be to observe that the concatenated string will be palindrome if the first character of first string matches with the last character of second string as we are considering prefix of first string and suffix of second string.
- Start iterating the first string from start using a pointer say i and second string from end using a pointer say j until i < j and s1[i] == s2[j].
- Check if both of the pointers i and j are equal at first mismatch.
- If yes, then return index i, that is we can concatenates strings s1[0..i] and s2[j..N] to form a palindrome.
- Otherwise, check if either s1[i..j] or s2[i..j] is a palindrome. If yes, then we can still concatenate either s1[0..j] + s2[j+1, N-1] or s1[0..i-1] + s2[i..N-1] to form a palindrome.
- Else, return -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string s, int i, int j)
{
while (i < j) {
if (s[i] != s[j])
return false ;
i++;
j--;
}
return true ;
}
int getIndex(string s1, string s2, int len)
{
int i = 0, j = len - 1;
while (i < j) {
if (s1[i] != s2[j]) {
break ;
}
i++;
j--;
}
if (i == j) {
return i - 1;
}
else if (isPalindrome(s2, i, j))
return i - 1;
else if (isPalindrome(s1, i, j))
return j;
return -1;
}
int main()
{
string s1 = "abcdf" , s2 = "sfgba" ;
int len = s1.length();
cout << getIndex(s1, s2, len);
return 0;
}
|
Java
class GFG
{
static boolean isPalindrome(String s, int i, int j)
{
while (i < j)
{
if (s.charAt(i) != s.charAt(j))
return false ;
i++;
j--;
}
return true ;
}
static int getIndex(String s1, String s2, int len)
{
int i = 0 , j = len - 1 ;
while (i < j)
{
if (s1.charAt(i) != s2.charAt(j))
{
break ;
}
i++;
j--;
}
if (i == j)
{
return i - 1 ;
}
else if (isPalindrome(s2, i, j))
return i - 1 ;
else if (isPalindrome(s1, i, j))
return j;
return - 1 ;
}
public static void main(String args[])
{
String s1 = "abcdf" , s2 = "sfgba" ;
int len = s1.length();
System.out.println( getIndex(s1, s2, len));
}
}
|
Python3
def isPalindrome(s, i, j) :
while (i < j) :
if (s[i] ! = s[j]) :
return False ;
i + = 1 ;
j - = 1 ;
return True ;
def getIndex(s1, s2, length) :
i = 0 ; j = length - 1 ;
while (i < j) :
if (s1[i] ! = s2[j]) :
break ;
i + = 1 ;
j - = 1 ;
if (i = = j) :
return i - 1 ;
elif (isPalindrome(s2, i, j)) :
return i - 1 ;
elif (isPalindrome(s1, i, j)) :
return j;
return - 1 ;
if __name__ = = "__main__" :
s1 = "abcdf" ;
s2 = "sfgba" ;
length = len (s1) ;
print (getIndex(s1, s2, length));
|
C#
using System;
class GFG
{
static bool isPalindrome( string s,
int i, int j)
{
while (i < j)
{
if (s[i] != s[j])
return false ;
i++;
j--;
}
return true ;
}
static int getIndex( string s1, string s2, int len)
{
int i = 0, j = len - 1;
while (i < j)
{
if (s1[i] != s2[j])
{
break ;
}
i++;
j--;
}
if (i == j)
{
return i - 1;
}
else if (isPalindrome(s2, i, j))
return i - 1;
else if (isPalindrome(s1, i, j))
return j;
return -1;
}
public static void Main()
{
string s1 = "abcdf" , s2 = "sfgba" ;
int len = s1.Length;
Console.WriteLine(getIndex(s1, s2, len));
}
}
|
Javascript
<script>
function isPalindrome(s, i, j)
{
while (i < j)
{
if (s[i] != s[j])
return false ;
i++;
j--;
}
return true ;
}
function getIndex(s1, s2, len)
{
let i = 0, j = len - 1;
while (i < j)
{
if (s1[i] != s2[j])
{
break ;
}
i++;
j--;
}
if (i == j)
{
return i - 1;
}
else if (isPalindrome(s2, i, j))
return i - 1;
else if (isPalindrome(s1, i, j))
return j;
return -1;
}
let s1 = "abcdf" , s2 = "sfgba" ;
let len = s1.length;
document.write(getIndex(s1, s2, len) + "</br>" );
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...