800. Delete to Match
Two archivists each hold a string of lowercase letters, a and b. They want both strings to end up exactly identical, but the only thing either of them may do is erase a single character from anywhere in their own string, one erasure at a time.
Return the minimum total number of erased characters, counted over both strings, after which the two strings are equal. It is fine for both strings to become empty, which is what happens when they share no letter at all.
Your solution should run in O(n*m) time, where n and m are the string lengths, and use O(min(n, m)) extra space.
Example 1
- Input:
- a = "pinecone", b = "opencone"
- Output:
- 4
- Explanation:
The two strings can be made equal with 4 deletions in total, by keeping a longest shared subsequence "pecone".
Example 2
- Input:
- a = "zebra", b = "lion"
- Output:
- 9
- Explanation:
The two strings can be made equal with 9 deletions in total, by keeping a longest shared subsequence "".
Example 3
- Input:
- a = "tartan", b = "tantra"
- Output:
- 4
- Explanation:
The two strings can be made equal with 4 deletions in total, by keeping a longest shared subsequence "tata".
Constraints
1 ≤ a.length, b.length ≤ 1000
Both strings contain only lowercase English letters.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n*m)
- Space
- O(min(n,m))