Skip to content
BytePatterns

Fewest Deletions to Match Two Words

EasyDynamic Programming#lcs#string-dp~20m

Problem

Given two words a and b, one step deletes a single character from either word. Return the fewest steps needed to make the two words identical.

Examples

Input:  a = "sea", b = "eat"
Output: 2
Why:    delete the s from sea and the t from eat, leaving ea in both
Input:  a = "abcde", b = "ace"
Output: 2
Why:    ace already appears inside abcde in order, so only b and d go
Input:  a = "abc", b = ""
Output: 3
Why:    edge case, everything in the first word must be deleted

Hints

0 / 3

Stuck on the idea rather than the code? Longest Common Subsequence covers it.