Counting Knight-Walk Phone Numbers on a Keypad
Dynamic ProgrammingAuto-graded · 19 test casesLast updated
Count length-n digit strings formed by knight jumps on a phone keypad, modulo 1e9+7, using DP over a small fixed graph.
Knight Walk Phone Numbers
Imagine a telephone keypad laid out like this:
1 2 3
4 5 6
7 8 9
* 0 #
A chess knight stands on one of the keys. It may only occupy the ten digit keys 0 through 9; the keys * and # are off limits. Each jump is a standard knight move: two cells in one direction (horizontal or vertical) and then one cell perpendicular to it.
Each time the knight lands on a key, you write down that digit. Starting on any digit key of your choice and making legal jumps, the digits you record form a number string. Your task is to count how many different digit strings of length exactly n can be produced this way.
Since the count can be huge, return it modulo 1000000007.
Details
- The first digit of the string is the key where the knight starts, so a string of length
ninvolves exactlyn - 1jumps. - Every one of the ten digits is a valid starting key, including
5. Note that5has no legal jumps, so it can only appear in a string whenn = 1. - Two strings are different if they differ at any position.
Function
Implement count_knight_numbers(n), which returns the number of valid length-n digit strings modulo 1000000007.
Constraints
1 <= n <= 5000- The result must be returned modulo
1000000007.
Examples
Example 1
Input: count_knight_numbers(1)
Output: 10
Example 2
Input: count_knight_numbers(2)
Output: 20
Example 3
Input: count_knight_numbers(3)
Output: 46
Hints
Hint 1
Think of the digits as nodes in a graph, with an edge wherever a knight can jump between two keys. A string of length n is a walk with n - 1 jumps.
Hint 2
Let ways[d] be the number of valid strings of the current length that end at digit d. Start with ways[d] = 1 for every digit, then build the next length from the previous one.
Hint 3
To extend by one digit, add ways[d] to each digit reachable from d. Reduce modulo 1000000007 at every addition, run the update n - 1 times, and sum the final ten values.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Model the keypad as a graph: the ten digits are nodes, and two digits are joined when a knight can jump between them. The neighbors of each digit are:
| Digit | Reachable digits |
|---|---|
| 0 | 4, 6 |
| 1 | 6, 8 |
| 2 | 7, 9 |
| 3 | 4, 8 |
| 4 | 0, 3, 9 |
| 5 | none |
| 6 | 0, 1, 7 |
| 7 | 2, 6 |
| 8 | 1, 3 |
| 9 | 2, 4 |
A string of length n is a walk through n nodes, so it uses n - 1 jumps. We use dynamic programming over the length of the string.
Let ways[d] be the number of valid strings of the current length that end at digit d.
- For length 1, every digit is a valid string, so
ways = [1] * 10. - To go from length
ktok + 1, every string ending atdcan be extended by any digit reachable fromd. So for each jumpd -> t, we addways[d]intonxt[t]. - After
n - 1such updates, the answer is the sum of all entries, taken modulo1000000007.
Why it is correct
Every valid string of length k + 1 splits uniquely into a prefix of length k (ending at some digit d) and one final jump from d to the last digit t. The update adds each (prefix, jump) pair exactly once, so nothing is missed and nothing is double counted. By induction on the length, ways is exact at every step.
Digit 5 has no outgoing or incoming jumps, so it contributes 1 at length 1 and 0 afterward, which is exactly the required behavior. Because modular addition is compatible with reducing intermediate values, taking the modulus at every addition gives the correct final residue.
Complexity
- Time: O(n * E), where E = 20 is the number of directed jumps. For a fixed keypad this is O(n).
- Space: O(1), since only two arrays of ten integers and the constant jump table are stored.
Reference solution
def count_knight_numbers(n: int) -> int:
mod = 1_000_000_007
# jumps[d] lists the digits a knight can reach from digit d in one move
jumps = ((4, 6), (6, 8), (7, 9), (4, 8), (0, 3, 9),
(), (0, 1, 7), (2, 6), (1, 3), (2, 4))
ways = [1] * 10
for _ in range(n - 1):
nxt = [0] * 10
for digit, targets in enumerate(jumps):
for t in targets:
nxt[t] = (nxt[t] + ways[digit]) % mod
ways = nxt
return sum(ways) % mod