Input: str1 = “programmingbloods”, str2 = “ibloods”
Substrings to be removed from str1 are [“programm”, “ng”]. Therefore, the length of the longest deleted substring is 8.
Input: str1=“GeeksforGeeks”, str2=“forks”
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:
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.
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum characters to be deleted from the end to make given two strings equal
- Find length of longest subsequence of one string which is substring of another string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Count columns to be deleted to make each row sorted
- Length of smallest substring of a given string which contains another string as subsequence
- Length of the longest substring with equal 1s and 0s
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Minimum length of substring whose rotation generates a palindromic substring
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Length of the longest substring without repeating characters
- Longest Even Length Substring such that Sum of First and Second Half is same
- Length of the longest substring that do not contain any palindrome
- Length of the longest substring with no consecutive same letters
- Length of longest substring having all characters as K
- Find length of longest substring with at most K normal characters
- Length of Longest Palindrome Substring
- Length of the longest valid substring
- Check if the given string is shuffled substring of another string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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