Dynamic programming · Medium
A text comparison tool reports how many single-character changes transform one short identifier into another. Its edit model is deliberately small: insert, delete or replace one character. It does not model word moves or spelling semantics.
Return the minimum number of allowed edits needed to turn a into b. Each edit costs one.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "a": string, "b": string }.
Fields
- a and b are printable ASCII strings. Matching is case-sensitive and spaces are ordinary characters.
Output
Return value
- Return one integer minimum cost. Equal strings return 0.
- Insertion adds one character, deletion removes one and replacement changes one.
- Swapping two adjacent characters is not a single operation.
- Transforming an empty string costs the length of the other string.
Constraints
- 0 <= a.length, b.length <= 2000.
- Characters have ASCII codes 32..126. Unicode normalization is outside this contract.
- Return only the cost; reconstructing the edit sequence is not required.
Examples
Example 1
{
"a": "kitten",
"b": "sitting"
}Returns
3Replace k with s, replace e with i and insert g.
Example 2
{
"a": "ab",
"b": "ba"
}Returns
2A transposition is not one allowed edit.
Required complexity
O(nm + n + m) time and O(min(n,m) + 1) extra space