Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if s is palindrome
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;
}
  
// Function to return the required index
int getIndex(string S1, string S2, int n)
{
  
    string S = "";
  
    for (int i = 0; i < n; i++) {
  
        // Copy the ith character in S
        S = S + S1[i];
        string Temp = "";
  
        // Copy all the character of string s2 in Temp
        for (int j = i + 1; j < n; j++)
            Temp += S2[j];
  
        // Check whether the string is palindrome
        if (isPalindrome(S + Temp)) {
            return i;
        }
    }
  
    return -1;
}
  
// Driver code
int main()
{
    string S1 = "abcdf", S2 = "sfgba";
    int n = S1.length();
  
    cout << getIndex(S1, S2, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function that returns true if s is palindrome
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;
}
  
// Function to return the required index
static int getIndex(String S1, String S2, int n)
{
  
    String S = "";
  
    for (int i = 0; i < n; i++)
    {
  
        // Copy the ith character in S
        S = S + S1.charAt(i);
        String Temp = "";
  
        // Copy all the character of string
        // s2 in Temp
        for (int j = i + 1; j < n; j++)
            Temp += S2.charAt(j);
  
        // Check whether the string is palindrome
        if (isPalindrome(S + Temp)) 
        {
            return i;
        }
    }
  
    return -1;
}
  
// Driver code
public static void main(String[] args)
{
    String S1 = "abcdf", S2 = "sfgba";
    int n = S1.length();
  
    System.out.println(getIndex(S1, S2, n));
}
}
  
// This code is contributed by Code_Mech.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class GFG
{
      
// Function that returns true if
// s is palindrome
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;
}
  
// Function to return the required index
static int getIndex(string S1, 
                    string S2, int n)
{
    string S = "";
  
    for (int i = 0; i < n; i++)
    {
  
        // Copy the ith character in S
        S = S + S1[i];
        string Temp = "";
  
        // Copy all the character of string
        // s2 in Temp
        for (int j = i + 1; j < n; j++)
            Temp += S2[j];
  
        // Check whether the string
        // is palindrome
        if (isPalindrome(S + Temp)) 
        {
            return i;
        }
    }
  
    return -1;
}
  
// Driver code
public static void Main()
{
    string S1 = "abcdf", S2 = "sfgba";
    int n = S1.Length;
  
    Console.WriteLine(getIndex(S1, S2, n));
}
}
  
// This code is contributed by Code_Mech.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function that returns true if s 
// is palindrome
function isPalindrome($s)
{
    $i = 0;
    $j = strlen($s) - 1;
  
    while ($i < $j
    {
        if ($s[$i] != $s[$j])
            return false;
        $i++;
        $j--;
    }
  
    return true;
}
  
// Function to return the required index
function getIndex($S1, $S2, $n)
{
    $S = "";
  
    for ($i = 0; $i < $n; $i++)
    {
  
        // Copy the ith character in S
        $S = $S . $S1[$i];
        $Temp = "";
  
        // Copy all the character of string
        // s2 in Temp
        for ($j = $i + 1; $j < $n; $j++)
            $Temp .= $S2[$j];
  
        // Check whether the string is palindrome
        if (isPalindrome($S . $Temp)) 
        {
            return $i;
        }
    }
  
    return -1;
}
  
// Driver code
$S1 = "abcdf"; $S2 = "sfgba";
$n = strlen($S1);
  
echo getIndex($S1, $S2, $n);
  
// This code is contributed 
// by Akanksha Rai
?>

chevron_right