583. Delete Operation for Two Strings
Delete Operation for Two Strings (LeetCode 583) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 2D Array (Longest Common Subsequence - LCS) subpattern.
How to solve Delete Operation for Two Strings
Delete Operation for Two Strings is a Longest Common Subsequence problem, part of Dynamic Programming (DP) Patterns. Find longest common subsequence between two strings using 2D DP.
Work towards the solution in these steps:
- Think about how dynamic programming can help solve problems involving comparing two strings efficiently.
- Consider the concept of the Longest Common Subsequence (LCS) between the two strings and how it relates to making the strings identical.
- Try to build a 2D DP array where each cell represents a subproblem comparing prefixes of the two strings.
The reference solution runs in O(m * n) time and O(n) space.
The Delete Operation for Two Strings solution and editorial walks through the full approach with reference code.
Problem statement
Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same.
In one step, you can delete exactly one character in either string.
Example 1:
Input: word1 = "sea", word2 = "eat" Output: 2 Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Example 2:
Input: word1 = "leetcode", word2 = "etco" Output: 4
Constraints:
- 1 <= word1.length, word2.length <= 500
- word1 and word2 consist of only lowercase English letters.
Topics and companies
Topics: String, Dynamic Programming.
Reported in interviews at Google.
How to practise Delete Operation for Two Strings on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(n) space.
Open Delete Operation for Two Strings in the code editor, or read the Delete Operation for Two Strings editorial for a worked solution with its approach and complexity analysis.
Where Delete Operation for Two Strings sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- 2D Array (Longest Common Subsequence - LCS) subpattern
- The full DSA patterns sheet
Problems related to Delete Operation for Two Strings
Other problems that use the same 2D Array (Longest Common Subsequence - LCS) and Dynamic Programming (DP) Patterns techniques:
- Edit Distance — Medium, same subpattern
- Minimum ASCII Delete Sum for Two Strings — Medium, same subpattern
- Regular Expression Matching — Hard, same subpattern
- Best Time to Buy and Sell Stock — Easy, same pattern
- Best Time to Buy and Sell Stock II — Medium, same pattern
- Best Time to Buy and Sell Stock III — Hard, same pattern
Delete Operation for Two Strings FAQ
What is the LeetCode number of Delete Operation for Two Strings?
Delete Operation for Two Strings is LeetCode problem 583, rated Medium.
What pattern is used to solve Delete Operation for Two Strings?
Delete Operation for Two Strings is a Longest Common Subsequence problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 2D Array (Longest Common Subsequence - LCS) subpattern.
What is the time complexity of Delete Operation for Two Strings?
The reference solution runs in O(m * n) time and O(n) space.
Which companies ask Delete Operation for Two Strings?
Delete Operation for Two Strings has been reported in interviews at Google.