- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Length of longest substring to be deleted to make a string equal to another string
In this article, we will discuss the problem of finding the length of the longest substring that needs to be deleted to make one string equal to another. We will first understand the problem statement and then explore both the naive and efficient approaches to solve this problem, along with their respective algorithms and time complexities. Lastly, we will implement the solution in C++.
Problem Statement
Given two strings, A and B, determine the length of the longest substring that needs to be deleted from string A to make it equal to string B.
Naive Approach
The naive approach is to generate all possible substrings of string A, delete each of them one by one, and check if the resultant string is equal to string B. If it is, we will store the length of the deleted substring. Finally, we will return the maximum length among all the deleted substrings.
Algorithm (Naive)
Initialize maxLength to 0.
Generate all possible substrings of string A
For each substring, delete it from string A and check if the resultant string is equal to string B.
If yes, update maxLength to the maximum of maxLength and the length of the deleted substring.
Return maxLength.
C++ Code (Naive)
Example
#include <iostream> #include <string> #include <algorithm> int longestSubstringToDelete(std::string A, std::string B) { int maxLength = 0; for (int i = 0; i < A.length(); i++) { for (int j = i; j < A.length(); j++) { std::string temp = A; temp.erase(i, j - i + 1); if (temp == B) { maxLength = std::max(maxLength, j - i + 1); } } } return maxLength; } int main() { std::string A = "abcde"; std::string B = "acde"; std::cout << "Length of longest substring to be deleted: " << longestSubstringToDelete(A, B) << std::endl; return 0; }
Output
Length of longest substring to be deleted: 1
Time Complexity (Naive) − O(n^3), where n is the length of string A.
Efficient Approach
The efficient approach to solve this problem is to find the longest common subsequence (LCS) of both strings. The length of the longest substring that needs to be deleted from string A to make it equal to string B is the difference between the length of string A and the length of the LCS.
Algorithm (Efficient)
Find the longest common subsequence (LCS) of string A and string B.
Return the difference between the length of string A and the length of the LCS.
C++ Code (Efficient)
#include <iostream> #include <string> #include <vector> #include <algorithm> int longestCommonSubsequence(std::string A, std::string B) { int m = A.length(); int n = B.length(); std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (A[i - 1] == B[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; } int longestSubstringToDelete(std::string A, std::string B) { int lcsLength = longestCommonSubsequence(A, B); return A.length() - lcsLength; } int main() { std::string A = "abcde"; std::string B = "acde"; std::cout << "Length of longest substring to be deleted: " << longestSubstringToDelete(A, B) << std::endl; return 0; }
Output
Length of longest substring to be deleted: 1
Time Complexity (Efficient) − O(m * n), where m is the length of string A and n is the length of string B.
Conclusion
In this article, we explored the problem of finding the length of the longest substring that needs to be deleted to make one string equal to another. We discussed both the naive and efficient approaches to solve this problem, along with their algorithms and time complexities. The efficient approach, which utilizes the longest common subsequence concept, provides a significant improvement in time complexity over the naive approach.