### PROBLEM LINK:

**Editorialist:** Jingbo Shang

### DIFFICULTY:

Easy - Medium

### PREREQUISITES:

Dynamic Programming

### PROBLEM:

Given 3 types of operations, determine the minimum cost of transforming string **S[1…n]** to **T[1…m]**.

### EXPLANATION:

This problem is similar to the Edit Distance Problem. But here, due to the different cost plan, we need to slightly modify the classical dynamic programming.

Let’s use **f*[j]** to stand for the minimum cost of transform **S[1…i]** to **T[1…j]**. In this problem, the insert/delete cost is dependent on the length.

Consider the insert operation:

```
f*[j - k] + min{k + x, y} --update--> f*[j]
```

And the delete operation:

```
f[i - k][j] + k + x --update--> f*[j]
```

Replace/Directly match

```
f*[j] + (S[i + 1] != T[j + 1]) --update--> f[i + 1][j + 1]
```

Since we need to enumerate “k”, the time complexi will be O(**n m^2 + n^2 m**).