/72. Edit Distance

72. Edit Distance

Medium
Strings60.2% acceptance

Given two strings source_string and target_string, compute the minimum number of operations required to transform source_string into target_string. The allowed operations are: insertion of a character, deletion of a character, and replacement of a character.

Example 1

Input: source_string=abc, target_string=yabd

Output: 2

Explanation: Replace a with y, insert d at the end.

Example 2

Input: source_string=kitten, target_string=sitting

Output: 3

Explanation: Replace k with s, replace e with i, insert g at the end.

Constraints

  • 0 <= len(source_string) <= 500
  • 0 <= len(target_string) <= 500
  • source_string and target_string consist only of lowercase English letters
Python (current runtime)

Case 1

Input: source_string='algorithm', target_string='logarithm'

Expected: 3

Case 2

Input: source_string='flaw', target_string='lawn'

Expected: 2

Case 3

Input: source_string='abcd', target_string='efgh'

Expected: 4

Case 4

Input: source_string='a', target_string='a'

Expected: 0

Case 5

Input: source_string='', target_string='abc'

Expected: 3