Skip to content

Commit fd30dfd

Browse files
authored
Create 1415.The k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n_v1.cpp
1 parent b8dee41 commit fd30dfd

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
unordered_set<string>rets;
3+
string ans;
4+
int N,K;
5+
public:
6+
string getHappyString(int n, int k)
7+
{
8+
string s;
9+
ans = "";
10+
N = n;
11+
K = k;
12+
DFS(0,s);
13+
return ans;
14+
}
15+
16+
void DFS(int cur, string s)
17+
{
18+
if (cur==N)
19+
{
20+
rets.insert(s);
21+
if (rets.size()==K)
22+
ans = s;
23+
return;
24+
}
25+
26+
if (ans!="") return;
27+
28+
if (cur==0)
29+
{
30+
for (auto ch='a'; ch<='c'; ch++)
31+
{
32+
s.push_back(ch);
33+
DFS(cur+1, s);
34+
s.pop_back();
35+
}
36+
}
37+
else
38+
{
39+
for (auto ch='a'; ch<='c'; ch++)
40+
{
41+
if (ch==s.back()) continue;
42+
s.push_back(ch);
43+
DFS(cur+1, s);
44+
s.pop_back();
45+
}
46+
}
47+
}
48+
};

0 commit comments

Comments
 (0)