-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1415.cpp
More file actions
33 lines (31 loc) · 979 Bytes
/
1415.cpp
File metadata and controls
33 lines (31 loc) · 979 Bytes
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
class Solution {
public:
void backtrack(vector<string> &result, string current, int pos, int n, vector<char> &alphabet){
if(pos==n){
result.push_back(current);
return;
}
for(int i=0; i<alphabet.size(); i++){
if(pos==0){
current=current+alphabet[i];
backtrack(result, current, pos+1, n, alphabet);
current.pop_back();
}
else if(alphabet[i]!=current[pos-1]){
current=current+alphabet[i];
backtrack(result, current, pos+1, n, alphabet);
current.pop_back();
}
}
}
string getHappyString(int n, int k) {
vector<char> alphabet {'a','b','c'};
vector<string> result;
backtrack(result, "", 0, n, alphabet);
if (k>result.size()){
return "";
}
sort(result.begin(), result.end());
return result[k-1];
}
};