Lv.3dpstring
Edit Distance
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Return the minimum number of single-character insertions, deletions and replacements that turn a into b.
Examples
| a | b | result |
|---|---|---|
"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
-
a.length,b.length, lowercase letters only
Expected complexity
- Time
- O(NM)
- Space
- O(M)
Examples
| a | b | Expected |
|---|---|---|
"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
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 9 | verdict only |
| performance | 60 | 4 | verdict only |