Edit Distance
Real-world version: Command Correction CostThe statement here is our own: the same inputs, outputs and technique.
A shell offers a suggestion whenever a command is mistyped, and it ranks candidate suggestions by how much repair work each one needs.
You are given the string typed, which is what the user entered, and the string intended, which is the candidate command. A repair is exactly one of these three keystroke-level operations:
- drop one character from anywhere in the current text
- add one character at any position in the current text
- overwrite one character of the current text with a different one
Return the least number of repairs that turns typed into intended. Either string may be empty, and both are made of lowercase letters.
Examples
Example 1
- Input
- typed = "grep", intended = "grape"
- Output
- 2
Overwrite the third character with a and add e at the tail; no single repair can close the gap.
Example 2
- Input
- typed = "flaot", intended = "float"
- Output
- 2
The two transposed characters each need an overwrite, since swapping a pair is not one of the allowed repairs.
Example 3
- Input
- typed = "", intended = "cat"
- Output
- 3
Starting from nothing, every character of the candidate has to be added.
Constraints
- 0 <= typed.length <= 500
- 0 <= intended.length <= 500
- typed and intended contain only lowercase English letters
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def correction_cost(typed: str, intended: str) -> int:public int correctionCost(String typed, String intended)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's