-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path19_The_kth_Lexicographical_String.cpp
82 lines (61 loc) · 2.62 KB
/
19_The_kth_Lexicographical_String.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 1415. The k-th Lexicographical String of All Happy Strings of Length n
// 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.
// Example 1:
// Input: n = 1, k = 3
// Output: "c"
// Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
// Example 2:
// Input: n = 1, k = 4
// Output: ""
// Explanation: There are only 3 happy strings of length 1.
// Example 3:
// Input: n = 3, k = 9
// Output: "cab"
// Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab"
// Constraints:
// 1 <= n <= 10
// 1 <= k <= 100
class Solution
{
int n2;
string dfs(string prefix, int n, int k) {
if (n == 0)
return prefix;
for (char c = 'a'; c <= 'c'; c++) {
if (!prefix.empty() && c == prefix.back())
continue;
int cnt = (1 << (n2 - prefix.length() - 1));
if (cnt >= k)
return dfs(prefix + c, n - 1, k);
else
k -= cnt;
}
return "";
}
public:
string getHappyString(int n, int k) {
n2 = n;
return dfs("", n, k);
}
};
/*
This code finds the kth lexicographically sorted happy string of length n. Here's how it works:
1. The class maintains n2 as a member variable to store the original length n.
2. The dfs function uses a recursive approach with parameters:
- prefix: current string being built
- n: remaining length needed
- k: position of string we're looking for
3. Base case: When n becomes 0, we've built a string of required length, so return it
4. For each character 'a' to 'c':
- Skip if current char equals last char of prefix (happy string rule)
- Calculate count of possible strings (cnt) from current position
- If k is within current count, recurse with new prefix
- Otherwise, subtract count from k and try next character
5. The getHappyString function initializes n2 and starts DFS with empty prefix
The solution uses bit manipulation (1 << x) to calculate possible strings count efficiently.
*/