Lv.2dpstring
Decode Ways
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Letters are encoded A → 1 … Z → 26 and concatenated into the digit string s. Return the number of ways to decode s,
modulo . A lone "0" or a two-digit piece starting with 0 cannot be decoded.
Examples
| s | result |
|---|---|
"12" | 2 |
"226" | 3 |
"06" | 0 |
"2101" | 1 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(s: string) → int
Constraints
-
s.length, digits only
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| s | Expected |
|---|---|
"12" | 2 |
"226" | 3 |
"06" | 0 |
"2101" | 1 |
Hint
Hint
ways[i] = (s[i-1] != '0' ? ways[i-1] : 0) + (10 <= s[i-2..i) <= 26 ? ways[i-2] : 0), ways[0] = 1, all modulo the prime.
Trying both readings recursively grows like Fibonacci on "111…1" and exceeds the time limit.
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 | 4 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 4 | verdict only |