Funcia
Lv.3dpstring

Edit Distance

Memory limit
256 MB
Correctness time limit
10000 ms
Solvers
0
0

Return the minimum number of single-character insertions, deletions and replacements that turn a into b.

Examples

abresult
"horse""ros"3
"intention""execution"5
"""abc"3

Efficiency

This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.

Function signature

solution(a: string, b: string) → int

Constraints

  • 0≤0 \le a.length, b.length ≤2,000\le 2{,}000, lowercase letters only

Expected complexity

Time
O(NM)
Space
O(M)

Examples

abExpected
"horse""ros"3
"intention""execution"5
"""abc"3

Hint

Hint

d[i][j] = cost for prefixes of length i and j: d[i-1][j-1] on a match, otherwise 1 + min(d[i-1][j], d[i][j-1], d[i-1][j-1]). Two rows suffice. Recursion without memoization is exponential.

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msestimated
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1400 msestimated
C# 14 (.NET 10)1400 msestimated

Test groups

GroupWeightTestsVisibility
sample03inputs and outputs
correctness409verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.