Android Unlock Patterns Problem
Description
LeetCode Problem 351.
Android devices have a special lock screen with a 3 x 3 grid of dots. Users can set an "unlock pattern" by connecting the dots in a specific sequence, forming a series of joined line segments where each segment's endpoints are two consecutive dots in the sequence. A sequence of k dots is a valid unlock pattern if both of the following are true:
- All the dots in the sequence are distinct.
- If the line segment connecting two consecutive dots in the sequence passes through the center of any other dot, the other dot must have previously appeared in the sequence. No jumps through the center non-selected dots are allowed.
- For example, connecting dots
2and9without dots5or6appearing beforehand is valid because the line from dot2to dot9does not pass through the center of either dot5or6. - However, connecting dots
1and3without dot2appearing beforehand is invalid because the line from dot1to dot3passes through the center of dot2.
- For example, connecting dots
Here are some example valid and invalid unlock patterns:

- The 1st pattern
[4,1,3,6]is invalid because the line connecting dots1and3pass through dot2, but dot2did not previously appear in the sequence. - The 2nd pattern
[4,1,9,2]is invalid because the line connecting dots1and9pass through dot5, but dot5did not previously appear in the sequence. - The 3rd pattern
[2,4,1,3,6]is valid because it follows the conditions. The line connecting dots1and3meets the condition because dot2previously appeared in the sequence. - The 4th pattern
[6,5,4,1,9,2]is valid because it follows the conditions. The line connecting dots1and9meets the condition because dot5previously appeared in the sequence.
Given two integers m and n, return the number of unique and valid unlock patterns of the Android grid lock screen that consist of at least m keys and at most n keys.
Two unlock patterns are considered unique if there is a dot in one sequence that is not in the other, or the order of the dots is different.
Example 1:
Input: m = 1, n = 1
Output: 9
Example 2:
Input: m = 1, n = 2
Output: 65
Constraints:
1 <= m, n <= 9
Solution
Approach: Backtracking with Symmetry
There are only 9 dots, so we can simply build every valid pattern with a depth-first search and count the ones whose length is between m and n.
The only tricky rule is the jump. A move from i to j passes through another dot in exactly these cases: along a row or column between two outer dots (1-3, 4-6, 7-9, 1-7, 2-8, 3-9), and along a diagonal between opposite corners (1-9, 3-7). A table skip[i][j] stores the dot in the middle (or 0 when there is none). Every other move, like 2 to 9 or 1 to 6, passes through no dot center and is always allowed.
From the current dot i, we may move to any unused dot j when skip[i][j] is 0 or that middle dot has already been used. The search marks i as used, counts the current path if its length is at least m, tries every allowed next dot, and unmarks i when it backtracks. It stops going deeper once the length would pass n.
The grid is symmetric, so we don't have to search from all 9 starting dots. The four corners 1, 3, 7, 9 all give the same count, and so do the four edges 2, 4, 6, 8. The answer is 4 * count(1) + 4 * count(2) + count(5).
When m > n, no length fits and the search returns 0.
Complexity: The search visits each valid pattern once. There are fewer than 9! of them (389,497 patterns in total), so the work is bounded by a constant. O(1) extra space for the table and the recursion.
class Solution:
def numberOfPatterns(self, m: int, n: int) -> int:
# skip[i][j]: the dot that lies between i and j (0 if none)
skip = [[0] * 10 for _ in range(10)]
for a, b, mid in ((1, 3, 2), (4, 6, 5), (7, 9, 8), (1, 7, 4), (2, 8, 5),
(3, 9, 6), (1, 9, 5), (3, 7, 5)):
skip[a][b] = skip[b][a] = mid
used = [False] * 10
def dfs(i: int, length: int) -> int:
# number of valid patterns that start with the current path, which ends at i
if length > n:
return 0
used[i] = True
count = 1 if length >= m else 0
for j in range(1, 10):
mid = skip[i][j]
if not used[j] and (mid == 0 or used[mid]):
count += dfs(j, length + 1)
used[i] = False
return count
# corners 1, 3, 7, 9 are symmetric, and so are edges 2, 4, 6, 8
return dfs(1, 1) * 4 + dfs(2, 1) * 4 + dfs(5, 1)