Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ABHIINAV12/fd68028d58f6c17896135679c879e3de to your computer and use it in GitHub Desktop.
Save ABHIINAV12/fd68028d58f6c17896135679c879e3de to your computer and use it in GitHub Desktop.
class Solution {
public:
stack<char> st;
vector<string> sp;
void rec(int curr,int n){
if(curr==n){
string temp="";
while(!st.empty()){
temp+=st.top();
st.pop();
}
reverse(temp.begin(),temp.end());
for(auto it: temp)
st.push(it);
sp.push_back(temp);
return ;
}
int miss= st.top()-'a';
for(int i=0;i<3;++i){
if(i==miss) continue;
st.push('a'+i);
rec(curr+1,n);
st.pop();
}
}
string getHappyString(int n, int k) {
sp.clear();
if(n==0) return "";
while(!st.empty())
st.pop();
st.push('a');
rec(1,n);
while(!st.empty())
st.pop();
st.push('b');
rec(1,n);
while(!st.empty())
st.pop();
st.push('c');
rec(1,n);
if(sp.size()<k) return "";
return sp[k-1];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment