## 351. Android Unlock Patterns

Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.

Rules for a valid pattern:

1. Each pattern must connect at least m keys and at most n keys.
2. All the keys must be distinct.
3. If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
4. The order of keys used matters.

Explanation:

| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |

Invalid move: 4 - 1 - 3 - 6
Line 1 - 3 passes through key 2 which had not been selected in the pattern.

Invalid move: 4 - 1 - 9 - 2
Line 1 - 9 passes through key 5 which had not been selected in the pattern.

Valid move: 2 - 4 - 1 - 3 - 6
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern

Valid move: 6 - 5 - 4 - 1 - 9 - 2
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.

Example:
Given m = 1, n = 1, return 9.

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

b'
\n
\n\n
\n

## Summary

\n

After Android launched its "unlock pattern" system to protect our smart phones from unauthorized access, the most common question that comes to one\'s mind is: How secure exactly are these patterns? The current article gives an answer to this question, as presenting an algorithm, which computes the number of all valid pattern combinations. It is intended for intermediate users and introduces the following ideas:\nBacktracking, Arrays.

\n

## Solution

\n
\n

#### Approach #1: (Backtracking) [Accepted]

\n

Algorithm

\n

The algorithm uses backtracking technique to enumerate all possible combinations of numbers where . During the generation of the recursive solution tree, the algorithm cuts all the branches which lead to patterns which doesn\'t satisfy the rules and counts only the valid patterns.\nIn order to compute a valid pattern, the algorithm performs the following steps:

\n
\n
• \n

Select a digit which is not used in the pattern till this moment. This is done with the help of a array which stores all available digits.

\n
• \n
• \n

We need to keep last inserted digit . The algorithm makes a check whether one of the following conditions is valid.

\n
\n
• \n

There is a knight move (as in chess) from towards or and are adjacent digits in a row, in a column. In this case the sum of both digits should be an odd number.

\n
• \n
• \n

The middle element in the line which connects and was previously selected. In case and are positioned at both ends of the diagonal, digit = 5 should be previously selected.

\n
• \n
• \n

\n and are adjacent digits in a diagonal

\n
• \n
\n
• \n
\n

In case one of the conditions above is satisfied, digit becomes part of partially generated valid pattern and the algorithm continues with the next candidate digit till the pattern is fully generated. Then it counts it.\nIn case none of the conditions are satisfied, the algorithm rejects the current digit , backtracks and continues to search for other valid digits among the unused ones.

\n

\n

Java

\n
public class Solution {\n\n    private boolean used[] = new boolean[9];\n\n    public int numberOfPatterns(int m, int n) {         \n        int res = 0;\n        for (int len = m; len <= n; len++) {                \n            res += calcPatterns(-1, len);\n            for (int i = 0; i < 9; i++) {                   \n                used[i] = false;\n            }            \n        }\n        return res;\n    }\n\n    private boolean isValid(int index, int last) {\n        if (used[index])\n            return false;\n        // first digit of the pattern    \n        if (last == -1)\n            return true;\n        // knight moves or adjacent cells (in a row or in a column)        \n        if ((index + last) % 2 == 1)\n            return true;\n        // indexes are at both end of the diagonals for example 0,0, and 8,8          \n        int mid = (index + last)/2;\n        if (mid == 4)\n            return used[mid];\n        // adjacent cells on diagonal  - for example 0,0 and 1,0 or 2,0 and //1,1\n        if ((index%3 != last%3) && (index/3 != last/3)) {\n            return true;\n        }\n        // all other cells which are not adjacent\n        return used[mid];\n    }\n\n    private int calcPatterns(int last, int len) {\n        if (len == 0)\n            return 1;    \n        int sum = 0;\n        for (int i = 0; i < 9; i++) {\n            if (isValid(i, last)) {\n                used[i] = true;\n                sum += calcPatterns(i, len - 1);\n                used[i] = false;                    \n            }\n        }\n        return sum;\n    }\n}\n
\n

Complexity Analysis

\n
\n
• \n

Time complexity : , where is maximum pattern length

\n

The algorithm computes each pattern once and no element can appear in the pattern twice. The time complexity is proportional to the number of the computed patterns. One upper bound of the number of all possible combinations is :

\n
• \n
\n

\n\n

\n
\n
• Space complexity : , where is maximum pattern length\nIn the worst case the maximum depth of recursion is . Therefore we need space used by the system recursive stack
• \n
\n

## Further Thoughts

\n

The algorithm above could be optimized if we consider the symmetry property of the problem. We notice that the number of valid patterns with first digit 1, 3, 7, 9 are the same. A similar observation is true for patterns which starts with digit 2, 4, 6, 8. Hence we only need to calculate one among each group and multiply by 4.

\n

You can find the optimized solution here.

\n

Analysis written by: @elmirap.

\n
'