-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTheKthLexicographicalStringOfAllHappyStringsOfLengthN.java
More file actions
56 lines (46 loc) · 2.11 KB
/
TheKthLexicographicalStringOfAllHappyStringsOfLengthN.java
File metadata and controls
56 lines (46 loc) · 2.11 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// A happy string is a string that:
// - consists only of letters of the set ['a', 'b', 'c'].
// - s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).
// For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings
// "aa", "baa" and "ababbc" are not happy strings.
// Given two integers n and k, consider a list of all happy strings of length n
// sorted in lexicographical order.
// Return the kth string of this list or return an empty string if there are
// less than k happy strings of length n.
// See: https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/
// See: https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/discuss/614867/Java-Simple-Backtracking
package leetcode.backtracking;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class TheKthLexicographicalStringOfAllHappyStringsOfLengthN {
private int counter = 0;
private String ans = "";
public String getHappyString(int n, int k) {
LinkedList<String> seq = new LinkedList<>();
seq.add("");
Map<String, String[]> map = new HashMap<>();
map.put("", new String[] { "a", "b", "c" });
map.put("a", new String[] { "b", "c" });
map.put("b", new String[] { "a", "c" });
map.put("c", new String[] { "a", "b" });
backtrack(n, k, seq, map);
return ans;
}
private void backtrack(int n, int k, LinkedList<String> seq, Map<String, String[]> map) {
if (counter > k) return;
if (seq.size() == n + 1) {
if (++counter == k) ans = String.join("", seq);
return;
}
for (String ch : map.get(seq.getLast())) {
seq.add(ch);
backtrack(n, k, seq, map);
seq.removeLast();
}
}
public static void main(String[] args) {
TheKthLexicographicalStringOfAllHappyStringsOfLengthN sln = new TheKthLexicographicalStringOfAllHappyStringsOfLengthN();
System.out.println(sln.getHappyString(3, 9));
}
}