|
| 1 | +package depth_first_search; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +/** |
| 7 | +* Created by gouthamvidyapradhan on 09/03/2019 |
| 8 | +* There is a box protected by a password. The password is n digits, where each letter can be one of the first k |
| 9 | +* digits 0, 1, ..., k-1. |
| 10 | +* |
| 11 | +* You can keep inputting the password, the password will automatically be matched against the last n digits entered. |
| 12 | +* |
| 13 | +* For example, assuming the password is "345", I can open it when I type "012345", but I enter a total of 6 digits. |
| 14 | +* |
| 15 | +* Please return any string of minimum length that is guaranteed to open the box after the entire string is inputted. |
| 16 | +* |
| 17 | +* Example 1: |
| 18 | +* Input: n = 1, k = 2 |
| 19 | +* Output: "01" |
| 20 | +* Note: "10" will be accepted too. |
| 21 | +* Example 2: |
| 22 | +* Input: n = 2, k = 2 |
| 23 | +* Output: "00110" |
| 24 | +* Note: "01100", "10011", "11001" will be accepted too. |
| 25 | +* Note: |
| 26 | +* n will be in the range [1, 4]. |
| 27 | +* k will be in the range [1, 10]. |
| 28 | +* k^n will be at most 4096. |
| 29 | +* |
| 30 | +* Solution O(n x k ^ n) Do a dfs and explore every possible states which form a n digit number with-in the given |
| 31 | +* range k. Maintain a 'result' string and keep appending the new digit in every state, if the total number of states |
| 32 | +* visited reaches k ^ n then, the result string will be the answer. |
| 33 | +*/ |
| 34 | +public class ingTheSafe { |
| 35 | + |
| 36 | +/** |
| 37 | +* Main method |
| 38 | +* |
| 39 | +* @param args |
| 40 | +*/ |
| 41 | +public static void main(String[] args) { |
| 42 | +System.out.println(new ingTheSafe().Safe(4, 5)); |
| 43 | +} |
| 44 | + |
| 45 | +public String Safe(int n, int k) { |
| 46 | +int states = getStates(n, k); |
| 47 | +int[] N = new int[k]; |
| 48 | +for (int i = 0; i < k; i++) { |
| 49 | +N[i] = i; |
| 50 | +} |
| 51 | +return generate(N, n, 0, 0, "", k, states); |
| 52 | +} |
| 53 | + |
| 54 | +private int getStates(int n, int k) { |
| 55 | +if (n == 0) return 1; |
| 56 | +if (n == 1) return k; |
| 57 | +int result = 1; |
| 58 | +for (int i = 0; i < n; i++) { |
| 59 | +result *= k; |
| 60 | +} |
| 61 | +return result; |
| 62 | +} |
| 63 | + |
| 64 | +private String generate(int[] N, int n, int i, int count, String num, int k, int states) { |
| 65 | +if (count == n) { |
| 66 | +return dfs(num, new StringBuilder(num), new HashSet<>(), k, states, 1); |
| 67 | +} else { |
| 68 | +for (int j = i; j < N.length; j++) { |
| 69 | +String result = generate(N, n, j, count + 1, num + String.valueOf(N[j]), k, states); |
| 70 | +if (!result.isEmpty()) { |
| 71 | +return result; |
| 72 | +} |
| 73 | +} |
| 74 | +} |
| 75 | +return ""; |
| 76 | +} |
| 77 | + |
| 78 | +private String dfs(String num, StringBuilder result, Set<String> done, int k, int states, int count) { |
| 79 | +done.add(num); |
| 80 | +if (states == count) { |
| 81 | +return result.toString(); |
| 82 | +} else { |
| 83 | +for (int i = 0; i < k; i++) { |
| 84 | +String newNum = (num + String.valueOf(i)); |
| 85 | +String newState = newNum.substring(1); |
| 86 | +if (!done.contains(newState)) { |
| 87 | +String retValue = dfs(newState, result.append(String.valueOf(i)), done, k, states, count + 1); |
| 88 | +if (!retValue.isEmpty()) { |
| 89 | +return retValue; |
| 90 | +} else { |
| 91 | +result.deleteCharAt(result.length() - 1); |
| 92 | +} |
| 93 | +} |
| 94 | +} |
| 95 | +} |
| 96 | +done.remove(num); |
| 97 | +return ""; |
| 98 | +} |
| 99 | +} |
0 commit comments