-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1415.cpp
68 lines (59 loc) · 1.46 KB
/
#1415.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
class Solution {
public:
void solve(int index,int size,vector<string>&ans,string &temp)
{
if(index==size)
{
ans.push_back(temp);
return;
}
if(index==0)
{
temp+='a';
solve(index+1,size,ans,temp);
temp.pop_back();
temp+='b';
solve(index+1,size,ans,temp);
temp.pop_back();
temp+='c';
solve(index+1,size,ans,temp);
temp.pop_back();
}
else
{
if(temp[index-1]!='a')
{
temp+='a';
solve(index+1,size,ans,temp);
temp.pop_back();
}
if(temp[index-1]!='b')
{
temp+='b';
solve(index+1,size,ans,temp);
temp.pop_back();
}
if(temp[index-1]!='c')
{
temp+='c';
solve(index+1,size,ans,temp);
temp.pop_back();
}
}
}
string getHappyString(int n, int k) {
vector<string>ans;
string temp="";
solve(0,n,ans,temp);
sort(ans.begin(),ans.end());
if(k>ans.size())
{
return "";
}
// for(auto child:ans)
// {
// cout<<child<<" ";
// }
return ans[k-1];
}
};