KatInterview

Minimum Cost to Transform One String into Another

Dynamic ProgrammingAuto-graded · 17 test cases
MediumAuto-gradedDynamic Programmingdynamic programmingstringedit distance

Last updated

Tests string DP by computing the minimum total cost to convert a source string into a target string using per-character insert, delete, and replace operations with custom costs.

Minimum Cost to Transform One String into Another

You are given two strings, source and target, and three dictionaries that describe the cost of editing characters:

  • insert_cost: maps a character c to the cost of inserting c into the string.
  • delete_cost: maps a character c to the cost of deleting c from the string.
  • replace_cost: maps a pair "ab" (a two-character string) to the cost of replacing character a with character b.

Your task is to find the minimum total cost to transform source into target using any sequence of the three operations above.

Operations

OperationMeaningCost
Insert cInsert character c at any positioninsert_cost.get(c, 0)
Delete cDelete character c from the current stringdelete_cost.get(c, 0)
Replace a → bReplace character a with character breplace_cost.get(a+b, 0)
  • Any character or pair not present in the respective dictionary has cost 0 for that operation.
  • A replace operation where a == b is allowed but pointless; its cost equals replace_cost.get(a+a, 0).
  • You want the minimum-cost sequence of operations (each applied one at a time) to turn source exactly into target.

Constraints

  • 0 ≤ len(source), len(target) ≤ 2000
  • All strings and dictionary keys consist of lowercase English letters only.
  • All cost values are non-negative integers.
  • insert_cost, delete_cost, and replace_cost contain only lowercase-letter keys / two-lowercase-letter keys respectively.
  • If both strings are empty, the answer is 0.
  • If source is empty, the only option is to insert every character of target.
  • If target is empty, the only option is to delete every character of source.

Examples

Example 1

Input: min_transform_cost('cat', 'cut', {'i': 2, 'u': 3}, {'a': 1, 't': 5}, {'au': 2, 'ua': 2})

Output: 0

Example 2

Input: min_transform_cost('abc', 'abc', {'a': 10}, {'a': 10}, {})

Output: 0

Example 3

Input: min_transform_cost('', 'hello', {'h': 1, 'e': 2, 'l': 3, 'o': 4}, {}, {})

Output: 13

Hints

Hint 1

Think of the classic edit-distance DP table where dp[i][j] is the minimum cost to transform source[:i] into target[:j]. What are the three transitions that fill each cell?

Hint 2

For each cell (i, j) you have three choices: delete source[i-1] (come from dp[i-1][j]), insert target[j-1] (come from dp[i][j-1]), or replace source[i-1] with target[j-1] (come from dp[i-1][j-1]). Replacing with the same character is valid but you should also consider if replace is cheaper or more expensive than delete+insert.

Hint 3

Make sure your base cases handle transforming an empty source into a non-empty target (sum of insert costs) and a non-empty source into an empty target (sum of delete costs).