-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find the String.java
33 lines (29 loc) · 989 Bytes
/
Find the String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution{
public String findString(int n, int k){
StringBuilder ans = new StringBuilder(), temp = new StringBuilder();
Map<String, Integer> mp = new HashMap<>();
for (int i = 0; i < n; i++)
ans.append('0');
temp.append(ans);
mp.put(temp.toString(), 1);
dfs(temp, ans, mp, k);
return ans.toString();
}
public void dfs(StringBuilder curr, StringBuilder ans, Map<String, Integer> mp, int k)
{
StringBuilder temp;
for (int i = k - 1; i >= 0; i--)
{
temp = new StringBuilder(curr.substring(1)); // leave first ch and take rest n-1 ch
//append next ch
temp.append((char) (i + '0'));
if (!mp.containsKey(temp.toString()))
{
mp.put(temp.toString(), 1);
ans.append((char) (i + '0'));
dfs(temp, ans, mp, k);
return;
}
}
}
}