- Difficulty: Medium
- Tags: LeetCode, Medium, Bit Manipulation, Dynamic Programming, Backtracking, Bitmask, leetcode-351, O(9^2 * 2^9), O(9 * 2^9), 🔒
Problem
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