Created
April 20, 2020 02:54
-
-
Save ABHIINAV12/fd68028d58f6c17896135679c879e3de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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