1415.cpp (596B)
1 class Solution { 2 vector<string> res; 3 string crnt; 4 5 void rec(int n, int k, char last = 'x') { 6 if (res.size() == k) return; 7 if (crnt.size() == n) { 8 res.push_back(crnt); 9 return; 10 } 11 crnt.push_back(' '); 12 for (char c = 'a'; c <= 'c'; c++) { 13 if (c == last) continue; 14 crnt.back() = c; 15 rec(n, k, c); 16 } 17 crnt.pop_back(); 18 } 19 20 public: 21 string getHappyString(int n, int k) { 22 if (k > 3 * (1 << n - 1)) return ""; 23 rec(n, k); 24 return res.back(); 25 } 26 };