2-D Dynamic Programming Two strings
567. Delete Operation for Two Strings
You are given two strings, word1 and word2. In one step you delete one character from either string.
Return the fewest steps that make the two strings equal. Deleting every character leaves an empty string, and two empty strings are equal, so there is always an answer.
Example 1
Input: word1 = "spare", word2 = "pears"
4Explanation: Delete the s and the e from "spare", and the e and the s from "pears". Both strings become "par".
Example 2
Input: word1 = "cat", word2 = "dog"
6Explanation: The strings share no letter, so every letter goes, and both end up empty.
Example 3
Input: word1 = "sun", word2 = "sunday"
3Explanation: Delete d, a and y from "sunday". Nothing needs to be deleted from "sun".
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ word1.length, word2.length ≤ 3000word1andword2hold only lowercase English letters.
Clarifying questions
What you would ask an interviewer before writing any code.