Sub topic 1
Turning One String into Another: The Three Edit Operations
Spell checkers suggest "receive" when you type "recieve". Version control tools show which lines changed between two files. DNA tools line up sequences that differ by a few mutations. All of them answer the same question: how much work does it take to turn one string into another? This chapter defines that work as a sequence of small, precise edits.
Prerequisites
This chapter assumes you can:
- Read and run basic Python
- Work with strings and lists, including indexing and slicing
- Follow a short function that loops over a list
Later chapters use Big-O notation, recursion, and dynamic programming. You do not need any of them here.
The Three Operations
An edit operation changes a string by one character. The standard set has three operations:
| Operation | What it does | Example |
|---|---|---|
| Insertion | Adds one character at some position | `cat` → `cart` |
| Deletion | Removes one character | `cart` → `cat` |
| Substitution | Replaces one character with another | `cat` → `cut` |
Each operation costs 1. If you substitute a character with itself, nothing changes, and we treat that as a free match.
Insertion and deletion undo each other. If you can delete r from cart to get cat, you can insert r into cat to get cart. Substitution can be undone by another substitution. Because of this symmetry, the cost of turning a into b equals the cost of turning b into a.
A Worked Example: kitten to sitting
Here is one way to turn kitten into sitting using three edits:
Many other sequences also work. You could delete all six letters of kitten and insert all seven letters of sitting, which takes 13 edits. Both sequences are valid, but the first one is much cheaper. The next chapter defines edit distance as the cost of the cheapest sequence.
Applying Edits in Code
The function below applies a list of edits. Each edit is a tuple (op, position, char).
def apply_edits(s, edits):
chars = list(s)
for op, pos, ch in edits:
if op == "ins":
chars.insert(pos, ch)
elif op == "del":
del chars[pos]
elif op == "sub":
chars[pos] = ch
else:
raise ValueError(f"unknown operation: {op}")
return "".join(chars)
edits = [("sub", 0, "s"), ("sub", 4, "i"), ("ins", 6, "g")]
result = apply_edits("kitten", edits)
assert result == "sitting"
print(result)Common Misconceptions
- "A substitution is just a delete plus an insert." You can get the same result that way, but it costs 2 instead of 1. Allowing substitution is what makes
cat→cutcost 1. - "Swapping two adjacent letters is one edit." With only these three operations, turning
abintobatakes two edits. A later chapter adds transposition as a fourth operation. - "Edits must go left to right." Order affects the positions you write down, but the final string and the total cost do not depend on it.
Exercise
Using the apply_edits format, write a two-edit sequence that turns "flaw" into "lawn".
Show answer
[("del", 0, ""), ("ins", 3, "n")]. Deleting position 0 gives "law", and inserting "n" at position 3 gives "lawn".
Exercise
Why does turning "ab" into "ba" take two edits with only insertion, deletion, and substitution?
Show answer
No single edit can do it. One insertion or deletion changes the length. One substitution changes only one position, and both positions differ. Two substitutions (a→b, then b→a) work, so the minimum is 2.