Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4b82f6

Browse files
Create The k-th Lexicographical String of All Happy Strings of Length n
1 parent e2dac2c commit c4b82f6

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
想法:用 Recursiving Backtracking 的方法,找到所有可能的字串,並在找到第 k 個時停止並回傳
2+
3+
Time Complexity : O(n * 2^n) for the fist letter has 3 options , after that , each letter have two options
4+
So there are n * 2^(n - 1) possible strings = O(n * 2^n)
5+
Space Complexity : O(2^n) for the recursion (recursion depth = O(n))
6+
7+
class Solution {
8+
public:
9+
string ans ;
10+
int count = 0 ;
11+
void constructHappyString(int n , int k , string& result , int index) {
12+
if (count >= k)
13+
return ;
14+
if (index == n) {
15+
if (++count == k)
16+
ans = result ;
17+
return ;
18+
}
19+
20+
for(char c = 'a' ; c <= 'c' ; c++) {
21+
if (index == 0 || result[index - 1] != c) {
22+
result[index] = c ;
23+
constructHappyString(n , k , result , index + 1) ;
24+
}
25+
}
26+
return ;
27+
}
28+
string getHappyString(int n, int k) {
29+
string result ;
30+
result.resize(n) ;
31+
constructHappyString(n , k , result , 0 ) ;
32+
return ans ;
33+
}
34+
};
35+
36+
// 法二:我們可以知道前 2^(n - 1) 個的第一個字母一定是 a , 2^(n - 1) + 1 ~ 2 * 2^(n - 1)的第一個字母是 b , 接著是 c
37+
因此,我們可以從 k 得知各個位置是什麼字母並輸出
38+
39+
Time Complexity : O(n) for constructing the string
40+
Space Complexity : O(n) for the answer string
41+
42+
class Solution {
43+
public:
44+
string getHappyString(int n, int k) {
45+
// Calculate the total number of happy strings of length n
46+
int total = 3 * (1 << (n - 1));
47+
48+
// If k is greater than the total number of happy strings, return an
49+
// empty string
50+
if (k > total) return "";
51+
52+
string result(n, 'a');
53+
54+
// Define mappings for the next smallest and greatest valid characters
55+
unordered_map<char, char> nextSmallest = {
56+
{'a', 'b'}, {'b', 'a'}, {'c', 'a'}};
57+
unordered_map<char, char> nextGreatest = {
58+
{'a', 'c'}, {'b', 'c'}, {'c', 'b'}};
59+
60+
// Calculate the starting indices for strings beginning with 'a', 'b',
61+
// and 'c'
62+
int startA = 1;
63+
int startB = startA + (1 << (n - 1));
64+
int startC = startB + (1 << (n - 1));
65+
66+
// Determine the first character based on the value of k
67+
if (k < startB) {
68+
result[0] = 'a';
69+
k -= startA;
70+
} else if (k < startC) {
71+
result[0] = 'b';
72+
k -= startB;
73+
} else {
74+
result[0] = 'c';
75+
k -= startC;
76+
}
77+
78+
// Iterate through the remaining positions in the result string
79+
for (int charIndex = 1; charIndex < n; charIndex++) {
80+
// Calculate the midpoint of the group for the current character
81+
// position
82+
int midpoint = (1 << (n - charIndex - 1));
83+
84+
// Determine the next character based on the value of k
85+
if (k < midpoint) {
86+
result[charIndex] = nextSmallest[result[charIndex - 1]];
87+
} else {
88+
result[charIndex] = nextGreatest[result[charIndex - 1]];
89+
k -= midpoint;
90+
}
91+
}
92+
93+
return result;
94+
}
95+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /