AlgoFrames

Delete operations for two strings

Standard · Write code · about 15 min

Return the fewest characters you must delete, from either word, to make word1 and word2 equal.

Example 1

Input:

["sea","eat"]

Output:

2

Delete s from sea and t from eat. Both become ea.

Example 2

Input:

["abc","def"]

Output:

6

No character is shared, so everything goes.

Constraints

  • 1 ≤ word1.length, word2.length ≤ 500
  • Both words use lowercase English letters.

Learn the idea first: String DP: LCS, edit distance, palindromic subsequence, a visual lesson.

More String DP: LCS, edit distance, palindromic subsequence problems