Skip to content

Commit 122cc36

Browse files
authored
Create 1415. The k-th Lexicographical String of All Happy Strings of … (#720)
2 parents 60d6fdc + 2481d94 commit 122cc36

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string s = "abc", ans;
4+
void backtrack(string cur, int &n, int &k, int &count)
5+
{
6+
if(count == k) return;
7+
if(cur.size() == n)
8+
{
9+
ans = cur, count += 1;
10+
return;
11+
}
12+
for(auto ch : s)
13+
{
14+
if(cur.size() and ch == cur.back()) continue;
15+
backtrack(cur + ch, n, k, count);
16+
}
17+
}
18+
string getHappyString(int n, int k)
19+
{
20+
int count = 0;
21+
backtrack("", n, k, count);
22+
return count == k ? ans : "";
23+
}
24+
};

0 commit comments

Comments
 (0)