leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1415.cpp (596B)
0 class Solution {
1 vector<string> res;
2 string crnt;
4 void rec(int n, int k, char last = 'x') {
5 if (res.size() == k) return;
6 if (crnt.size() == n) {
7 res.push_back(crnt);
8 return;
9 }
10 crnt.push_back(' ');
11 for (char c = 'a'; c <= 'c'; c++) {
12 if (c == last) continue;
13 crnt.back() = c;
14 rec(n, k, c);
15 }
16 crnt.pop_back();
17 }
19 public:
20 string getHappyString(int n, int k) {
21 if (k > 3 * (1 << n - 1)) return "";
22 rec(n, k);
23 return res.back();
24 }
25 };