-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1415.cpp
More file actions
33 lines (33 loc) · 907 Bytes
/
1415.cpp
File metadata and controls
33 lines (33 loc) · 907 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
32
33
class Solution {
public:
priority_queue<string> pq;
vector<char> chars = {'a', 'b', 'c'};
void backtracking(int n, int k, string& s) {
if (s.size() == n) {
pq.push(s);
if (pq.size() > k) pq.pop();
}
else {
if (s.size() == 0) {
for (auto ch : chars) {
s.push_back(ch);
backtracking(n, k, s);
s.pop_back();
}
}
else {
for (auto ch : chars) {
if (s.back() == ch) continue;
s.push_back(ch);
backtracking(n, k, s);
s.pop_back();
}
}
}
}
string getHappyString(int n, int k) {
string s = "";
backtracking(n, k, s);
return pq.size() < k ? "" : pq.top();
}
};