Length of longest substring to be deleted to make a string equal to another string
Last Updated :
30 May, 2022
Given two strings str1 and str2, where str2 is a subsequence of str1, the task is to find the length of the longest substring of str1 which when removed, makes the strings str2 and str1 equal.
Examples:
Input: str1 = “programmingbloods”, str2 = “ibloods”
Output: 8
Explanation:
Substrings to be removed from str1 are [“program”, “ng”]. Therefore, the length of the longest deleted substring is 8.
Input: str1=“GeeksforGeeks”, str2=“forks”
Output: 5
Naive Approach: The simplest approach is to generate all possible substrings of str1 and for each substring, remove it from str1 and check if the resulting string becomes equal to str2 or not. Print the length of longest such substrings.
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to find the occurrence of str2 in str1. Traverse both strings from left to right and check if both characters are equal or not. If found to be true, proceed to the right in both strings. Otherwise, proceed to the right only in str2. Similarly, to find the last occurrence of str2 in str1, traverse both strings from right to left and proceed similarly. Follow the steps below to solve the problem:
- Initialize a variable, res=0 to store the length of the longest deleted substring.
- Create an array, pos[] to store the position of the first occurrence of str2 in str1.
- Traverse both strings from left to right and store the position of the first occurrence of str2 by deleting some characters of str1.
- Initialize a variable, lastPos = length(str1)-1 to store the position of the current character in the last occurrence of str2 by deleting some characters of str1.
- Traverse both strings from right to left and check if both characters match then find the position of the current character of str2 in pos[] else continue.
- If res > (lastPos – pos[i-1]-1) then update the res = lastPos – pos[i-1]-1.
- Finally, return the res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longDelSub(string str1, string str2)
{
int N = str1.size();
int M = str2.size();
int prev_pos = 0;
int pos[M];
for ( int i = 0; i < M; i++) {
int index = prev_pos;
while (index < N
&& str1[index] != str2[i]) {
index++;
}
pos[i] = index;
prev_pos = index + 1;
}
int res = N - prev_pos;
prev_pos = N - 1;
for ( int i = M - 1; i >= 0; i--) {
int index = prev_pos;
while (index >= 0
&& str1[index] != str2[i]) {
index--;
}
if (i != 0) {
res = max(
res,
index - pos[i - 1] - 1);
}
prev_pos = index - 1;
}
res = max(res, prev_pos + 1);
return res;
}
int main()
{
string str1 = "GeeksforGeeks" ;
string str2 = "forks" ;
cout << longDelSub(str1, str2);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int longDelSub(String str1, String str2)
{
int N = str1.length();
int M = str2.length();
int prev_pos = 0 ;
int pos[] = new int [M];
for ( int i = 0 ; i < M; i++)
{
int index = prev_pos;
while (index < N &&
str1.charAt(index) !=
str2.charAt(i))
{
index++;
}
pos[i] = index;
prev_pos = index + 1 ;
}
int res = N - prev_pos;
prev_pos = N - 1 ;
for ( int i = M - 1 ; i >= 0 ; i--)
{
int index = prev_pos;
while (index >= 0 &&
str1.charAt(index) !=
str2.charAt(i))
{
index--;
}
if (i != 0 )
{
res = Math.max(res,
index -
pos[i - 1 ] - 1 );
}
prev_pos = index - 1 ;
}
res = Math.max(res, prev_pos + 1 );
return res;
}
public static void main (String[] args)
{
String str1 = "GeeksforGeeks" ;
String str2 = "forks" ;
System.out.print(longDelSub(str1, str2));
}
}
|
Python3
def longDelSub(str1, str2):
N = len (str1)
M = len (str2)
prev_pos = 0
pos = [ 0 ] * M
for i in range (M):
index = prev_pos
while (index < N and
str1[index] ! = str2[i]):
index + = 1
pos[i] = index
prev_pos = index + 1
res = N - prev_pos
prev_pos = N - 1
for i in range (M - 1 , - 1 , - 1 ):
index = prev_pos
while (index > = 0 and
str1[index] ! = str2[i]):
index - = 1
if (i ! = 0 ) :
res = max (res,
index -
pos[i - 1 ] - 1 )
prev_pos = index - 1
res = max (res, prev_pos + 1 )
return res
str1 = "GeeksforGeeks"
str2 = "forks"
print (longDelSub(str1, str2))
|
C#
using System;
class GFG{
static int longDelSub( string str1,
string str2)
{
int N = str1.Length;
int M = str2.Length;
int prev_pos = 0;
int [] pos = new int [M];
for ( int i = 0; i < M; i++)
{
int index = prev_pos;
while (index < N &&
str1[index] != str2[i])
{
index++;
}
pos[i] = index;
prev_pos = index + 1;
}
int res = N - prev_pos;
prev_pos = N - 1;
for ( int i = M - 1; i >= 0; i--)
{
int index = prev_pos;
while (index >= 0 &&
str1[index] != str2[i])
{
index--;
}
if (i != 0)
{
res = Math.Max(res,
index -
pos[i - 1] - 1);
}
prev_pos = index - 1;
}
res = Math.Max(res, prev_pos + 1);
return res;
}
public static void Main()
{
string str1 = "GeeksforGeeks" ;
string str2 = "forks" ;
Console.Write(longDelSub(str1, str2));
}
}
|
Javascript
<script>
function longDelSub( str1, str2)
{
var N = str1.length;
var M = str2.length;
var prev_pos = 0;
var pos = new Array(M);
for (let i = 0; i < M; i++) {
var index = prev_pos;
while (index < N
&& str1[index] != str2[i]) {
index++;
}
pos[i] = index;
prev_pos = index + 1;
}
var res = N - prev_pos;
prev_pos = N - 1;
for (let i = M - 1; i >= 0; i--) {
var index = prev_pos;
while (index >= 0
&& str1[index] != str2[i]) {
index--;
}
if (i != 0) {
res = Math.max(
res,
index - pos[i - 1] - 1);
}
prev_pos = index - 1;
}
res = Math.max(res, prev_pos + 1);
return res;
}
var str1 = "GeeksforGeeks" ;
var str2 = "forks" ;
console.log(longDelSub(str1, str2));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(M)
Share your thoughts in the comments
Please Login to comment...