![]() |
VOOZH | about |
Given a mobile numeric keypad and an integer n, the task is to find the number of possible unique numeric sequences of length n that can be formed by pressing the keys. The sequences can be built by starting from any digit (0–9) on the keypad.
At each step, you are allowed to press the same key again or move to an adjacent key in the up, down, left, or right direction. However, diagonal movements and pressing the bottom row corner buttons (* and #) are not allowed.
Examples:
Input: n = 1
Output: 10
Explanation: The possible outputs are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.Input: n = 2
Output: 36
Table of Content
The idea is to use a recursive approach that takes the current cell position (i, j) and the remaining length of the number (n) as input. The base cases are when the current cell is invalid (i.e., outside the keypad or in the bottom row corners) or when the remaining length is 1, in which case we return 1.
For the recursive case, we check all 5 possible next cells (the current cell and the cells up, down, left, and right of it) and recursively call each of them, decrementing the remaining length by 1. The sum of the results from these 5 recursive calls is the answer for the current cell and remaining length.
Mathematically the recurrence relation will look like the following:
getCount(i, j, n) = sum(getCount(x, y, n-1)) where x, y are the next 5 possible cells.
Base Cases:
- getCount(i, j, n) = 0, if cell is invalid or cell is * or #.
- getCount(i, j, n) = 1, if n = 1.
10
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: Number of possible unique sequences of length n at cell (i, j), getCount(i, j, n) depends on the optimal solutions of getCount(x, y, n-1), where x, y are the neighbouring 5 cells.
2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times. For example, for getCount(0, 0, 5) and getCount(1, 1, 5), getCount(0, 1, 4) is called twice.
10
We use a 3D array dp[k][i][j], where k represents the length of the sequence, and i and j represent the row and column of the keypad, respectively. The DP array is of size (n+1) x 4 x 3, where n is the length of the sequence. The idea is to fill the table iteratively, starting from sequences of length 1 (which is initialized with 1 for all keys, except * and #) and then for each subsequent length, compute the possible sequences by considering adjacent keys (up, down, left, right).
10
We only need to maintain two 2D arrays, prev and curr, to track the state of each key on the keypad at each step. Initially, all keys are assumed to be reachable in one move, except for the corner keys * and #, which are set to 0. Then, for each step from 2 to n, calculate the number of ways to reach each key by considering its valid neighborsfrom the previous step.
After updating the curr array for each step, we copy its values to prev for use in the next iteration. Finally, we sum the values in prev after n steps to get the total number of unique sequences of length n that can be formed.
10