Android Unlock Patterns - Solution & Explanation
Problem Statement
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
Approach Overview
Problem Overview: Count how many valid unlock patterns can be formed on the Android 3x3 lock screen using lengths between m and n. A pattern cannot revisit a key, and moves that jump over another key are only allowed if that intermediate key has already been used.
Approach 1: Brute Force Backtracking (O(9!))
The straightforward strategy generates every possible path on the 3×3 grid using depth‑first search. Start from each digit (1–9), mark it visited, and recursively try the remaining digits while respecting the Android rule: if a move crosses another key (for example 1 → 3 crossing 2), that intermediate key must already be visited. A small lookup table called skip stores these required intermediate nodes. The search continues until the path length reaches n, counting patterns whose length is at least m. Time complexity is O(9!) in the worst case because permutations of keys are explored, and space complexity is O(9) for the recursion stack and visited array.
Approach 2: Symmetry Optimized Backtracking (O(9!))
The grid has strong symmetry. Corners (1,3,7,9) behave the same, edges (2,4,6,8) behave the same, and the center (5) is unique. Instead of running DFS from all nine digits, run it once from a representative of each group and multiply the result. For example, compute patterns starting from 1 and multiply by four for the corners. Do the same for 2 multiplied by four for edges, and handle 5 once. The same skip matrix enforces the rule about crossing intermediate keys. This reduces redundant exploration while keeping the same theoretical complexity O(9!), with space O(9). This approach is what most production solutions use because it cuts the constant factor dramatically.
Approach 3: Bitmask + DP State Exploration (O(9 * 2^9))
A more algorithmic perspective models the problem as state transitions. Each state is defined by the current key and a bitmask representing visited keys. From a state (node, mask), iterate over all possible next digits and check whether the move is valid using the same intermediate-key rule. Memoizing results for identical states avoids recomputation. The number of states is bounded by 9 * 2^9, giving time complexity around O(9 * 2^9) and space complexity O(9 * 2^9). This version connects naturally to dynamic programming and bitmask techniques.
Recommended for interviews: Symmetry‑optimized backtracking is the expected solution. It demonstrates control of DFS, constraint handling, and pruning using problem structure. Interviewers like seeing the skip matrix plus the symmetry observation because it reduces work from nine starting searches to three. Mentioning the bitmask state idea also shows familiarity with backtracking and DP tradeoffs.
Solution
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Backtracking | O(9!) | O(9) | Understanding the full search space and Android movement rules |
| Symmetry Optimized Backtracking | O(9!) | O(9) | Best practical solution; reduces repeated DFS using grid symmetry |
| Bitmask Dynamic Programming | O(9 * 2^9) | O(9 * 2^9) | When modeling states formally or demonstrating DP with bitmasking |
Video Solution
LeetCode 351. Android Unlock Patterns • Happy Coding • 6,430 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Android Unlock Patterns easy or hard?
Android Unlock Patterns Python/Java solution
How to solve Android Unlock Patterns in O(n)?
What is the best approach for Android Unlock Patterns?
Is Android Unlock Patterns asked at Google/Amazon/Meta?
What data structure is used in Android Unlock Patterns?
What is the time complexity of Android Unlock Patterns?
Ready to solve this problem?
Practice Android Unlock Patterns with our built-in code editor and test cases.
Practice on FleetCode