# Length of longest substring to be deleted to make a string equal to another string

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 [“programm”, “ng”]. Therefore, the length of the longest deleted substring is 8.

Input: str1=“GeeksforGeeks”, str2=“forks”
Output: 5

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. Initialize a variable, res=0 to store the length of the longest deleted substring.
2. Create an array, pos[] to store the position of the first occurrence of str2 in str1.
3. Traverse both strings from left to right and store the position of the first occurrence of str2 by deleting some characters of str1.
4. 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.
5. 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.
6. If res > (lastPospos[i-1]-1) then update the res = lastPos – pos[i-1]-1.
7. Finally, return the res.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement ` `// the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print the length ` `// of longest substring to be deleted ` `int` `longDelSub(string str1, string str2) ` `{ ` `    ``// Stores the length of string ` `    ``int` `N = str1.size(); ` `    ``int` `M = str2.size(); ` ` `  `    ``// Store the position of ` `    ``// previous matched ` `    ``// character of str1 ` `    ``int` `prev_pos = 0; ` ` `  `    ``// Store the position of first ` `    ``// occurrence of str2 in str1 ` `    ``int` `pos[M]; ` ` `  `    ``// Find the position of the ` `    ``// first occurrence of str2 ` `    ``for` `(``int` `i = 0; i < M; i++) { ` ` `  `        ``// Store the index of str1 ` `        ``int` `index = prev_pos; ` ` `  `        ``// If both characters not matched ` `        ``while` `(index < N ` `               ``&& str1[index] != str2[i]) { ` `            ``index++; ` `        ``} ` ` `  `        ``pos[i] = index; ` `        ``prev_pos = index + 1; ` `    ``} ` ` `  `    ``// Store the length of the ` `    ``// longest deleted substring ` `    ``int` `res = N - prev_pos; ` ` `  `    ``prev_pos = N - 1; ` ` `  `    ``// Store the position of last ` `    ``// occurrence of str2 in str1 ` `    ``for` `(``int` `i = M - 1; i >= 0; i--) { ` `        ``int` `index = prev_pos; ` ` `  `        ``// If both characters not matched ` `        ``while` `(index >= 0 ` `               ``&& str1[index] != str2[i]) { ` `            ``index--; ` `        ``} ` ` `  `        ``// Update res ` `        ``if` `(i != 0) { ` `            ``res = max( ` `                ``res, ` `                ``index - pos[i - 1] - 1); ` `        ``} ` ` `  `        ``prev_pos = index - 1; ` `    ``} ` ` `  `    ``// Update res. ` `    ``res = max(res, prev_pos + 1); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given string ` `    ``string str1 = ``"GeeksforGeeks"``; ` `    ``string str2 = ``"forks"``; ` ` `  `    ``// Function Call ` `    ``cout << longDelSub(str1, str2); ` `    ``return` `0; ` `} `

## Java

 `// Java program to implement  ` `// the above approach  ` `import` `java.io.*; ` ` `  `class` `GFG{ ` ` `  `// Function to print the length ` `// of longest substring to be deleted ` `static` `int` `longDelSub(String str1, String str2) ` `{ ` `     `  `    ``// Stores the length of string ` `    ``int` `N = str1.length(); ` `    ``int` `M = str2.length(); ` ` `  `    ``// Store the position of ` `    ``// previous matched ` `    ``// character of str1 ` `    ``int` `prev_pos = ``0``; ` ` `  `    ``// Store the position of first ` `    ``// occurrence of str2 in str1 ` `    ``int` `pos[] = ``new` `int``[M]; ` ` `  `    ``// Find the position of the ` `    ``// first occurrence of str2 ` `    ``for``(``int` `i = ``0``; i < M; i++) ` `    ``{ ` `         `  `        ``// Store the index of str1 ` `        ``int` `index = prev_pos; ` ` `  `        ``// If both characters not matched ` `        ``while` `(index < N &&  ` `               ``str1.charAt(index) !=  ` `               ``str2.charAt(i)) ` `        ``{ ` `            ``index++; ` `        ``} ` ` `  `        ``pos[i] = index; ` `        ``prev_pos = index + ``1``; ` `    ``} ` ` `  `    ``// Store the length of the ` `    ``// longest deleted substring ` `    ``int` `res = N - prev_pos; ` ` `  `    ``prev_pos = N - ``1``; ` ` `  `    ``// Store the position of last ` `    ``// occurrence of str2 in str1 ` `    ``for``(``int` `i = M - ``1``; i >= ``0``; i--) ` `    ``{ ` `        ``int` `index = prev_pos; ` ` `  `        ``// If both characters not matched ` `        ``while` `(index >= ``0` `&&  ` `               ``str1.charAt(index) !=  ` `               ``str2.charAt(i))  ` `        ``{ ` `            ``index--; ` `        ``} ` ` `  `        ``// Update res ` `        ``if` `(i != ``0``) ` `        ``{ ` `            ``res = Math.max(res,  ` `                           ``index -  ` `                           ``pos[i - ``1``] - ``1``); ` `        ``} ` `        ``prev_pos = index - ``1``; ` `    ``} ` ` `  `    ``// Update res. ` `    ``res = Math.max(res, prev_pos + ``1``); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` `     `  `    ``// Given string ` `    ``String str1 = ``"GeeksforGeeks"``; ` `    ``String str2 = ``"forks"``; ` ` `  `    ``// Function call ` `    ``System.out.print(longDelSub(str1, str2)); ` `} ` `} ` ` `  `// This code is contributed by code_hunt `

## Python3

 `# Python3 program to implement  ` `# the above approach  ` ` `  `# Function to prthe length ` `# of longest substring to be deleted ` `def` `longDelSub(str1, str2): ` `     `  `    ``# Stores the length of string ` `    ``N ``=` `len``(str1) ` `    ``M ``=` `len``(str2) ` ` `  `    ``# Store the position of ` `    ``# previous matched ` `    ``# character of str1 ` `    ``prev_pos ``=` `0` ` `  `    ``# Store the position of first ` `    ``# occurrence of str2 in str1 ` `    ``pos ``=` `[``0``] ``*` `M ` ` `  `    ``# Find the position of the ` `    ``# first occurrence of str2 ` `    ``for` `i ``in` `range``(M): ` ` `  `        ``# Store the index of str1 ` `        ``index ``=` `prev_pos ` ` `  `        ``# If both characters not matched ` `        ``while` `(index < N ``and`  `          ``str1[index] !``=` `str2[i]): ` `            ``index ``+``=` `1` `         `  `        ``pos[i] ``=` `index ` `        ``prev_pos ``=` `index ``+` `1` `     `  `    ``# Store the length of the ` `    ``# longest deleted substring ` `    ``res ``=` `N ``-` `prev_pos ` ` `  `    ``prev_pos ``=` `N ``-` `1` ` `  `    ``# Store the position of last ` `    ``# occurrence of str2 in str1 ` `    ``for` `i ``in` `range``(M ``-` `1``, ``-``1``, ``-``1``): ` `        ``index ``=` `prev_pos ` ` `  `        ``# If both characters not matched ` `        ``while` `(index >``=` `0` `and`  `          ``str1[index] !``=` `str2[i]): ` `            ``index ``-``=` `1` `         `  `        ``# Update res ` `        ``if` `(i !``=` `0``) : ` `            ``res ``=` `max``(res,  ` `                      ``index ``-`  `                      ``pos[i ``-` `1``] ``-` `1``) ` `         `  `        ``prev_pos ``=` `index ``-` `1` `     `  `    ``# Update res. ` `    ``res ``=` `max``(res, prev_pos ``+` `1``) ` ` `  `    ``return` `res ` ` `  `# Driver Code ` ` `  `# Given string ` `str1 ``=` `"GeeksforGeeks"` `str2 ``=` `"forks"` ` `  `# Function call ` `print``(longDelSub(str1, str2)) ` ` `  `# This code is contributed by code_hunt `

## C#

 `// C# program to implement  ` `// the above approach  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Function to print the length ` `// of longest substring to be deleted ` `static` `int` `longDelSub(``string` `str1, ` `                      ``string` `str2) ` `{ ` `     `  `    ``// Stores the length of string ` `    ``int` `N = str1.Length; ` `    ``int` `M = str2.Length; ` ` `  `    ``// Store the position of ` `    ``// previous matched ` `    ``// character of str1 ` `    ``int` `prev_pos = 0; ` ` `  `    ``// Store the position of first ` `    ``// occurrence of str2 in str1 ` `    ``int``[] pos = ``new` `int``[M]; ` ` `  `    ``// Find the position of the ` `    ``// first occurrence of str2 ` `    ``for``(``int` `i = 0; i < M; i++)  ` `    ``{ ` ` `  `        ``// Store the index of str1 ` `        ``int` `index = prev_pos; ` ` `  `        ``// If both characters not matched ` `        ``while` `(index < N &&  ` `          ``str1[index] != str2[i]) ` `        ``{ ` `            ``index++; ` `        ``} ` ` `  `        ``pos[i] = index; ` `        ``prev_pos = index + 1; ` `    ``} ` ` `  `    ``// Store the length of the ` `    ``// longest deleted substring ` `    ``int` `res = N - prev_pos; ` ` `  `    ``prev_pos = N - 1; ` ` `  `    ``// Store the position of last ` `    ``// occurrence of str2 in str1 ` `    ``for``(``int` `i = M - 1; i >= 0; i--) ` `    ``{ ` `        ``int` `index = prev_pos; ` ` `  `        ``// If both characters not matched ` `        ``while` `(index >= 0 &&  ` `          ``str1[index] != str2[i]) ` `        ``{ ` `            ``index--; ` `        ``} ` ` `  `        ``// Update res ` `        ``if` `(i != 0) ` `        ``{ ` `            ``res = Math.Max(res,  ` `                           ``index -  ` `                           ``pos[i - 1] - 1); ` `        ``} ` `        ``prev_pos = index - 1; ` `    ``} ` ` `  `    ``// Update res. ` `    ``res = Math.Max(res, prev_pos + 1); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `     `  `    ``// Given string ` `    ``string` `str1 = ``"GeeksforGeeks"``; ` `    ``string` `str2 = ``"forks"``; ` ` `  `    ``// Function call ` `    ``Console.Write(longDelSub(str1, str2)); ` `} ` `} ` ` `  `// This code is contributed by code_hunt`

Output:

```5
```

Time Complexity: O(N + M)
Auxiliary Space: O(M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : code_hunt

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.