4
public String crackSafe(int n, int k) {5
int minLen = (int) Math.pow(k, n) + (n - 1);7
dfs("", n, k, new HashSet<String>(), minLen);11
private void dfs(String s, int n, int k, HashSet<String> visited, int minLen) {12
if (s.length() == minLen) {16
if (s.length() > minLen) {20
for (int i = 0; i < k; i++) {21
s += String.valueOf(i);22
String lastN = s.substring(Math.max(0, s.length() - n), s.length());23
// If already in hashset, rollback and continue;24
if (visited.contains(lastN)) {25
s = s.substring(0, s.length() - 1);28
if (lastN.length() == n) { // only put n length string in hashset32
dfs(s, n, k, visited, minLen);33
if (visited.size() == minLen - n + 1) {35
} // if hashset contains all possible combinations just return36
visited.remove(lastN);37
s = s.substring(0, s.length() - 1);