Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created February 14, 2019 13:44
Show Gist options
  • Save fpdjsns/4508c71f410f330c4d46a8b4319a3f03 to your computer and use it in GitHub Desktop.
Save fpdjsns/4508c71f410f330c4d46a8b4319a3f03 to your computer and use it in GitHub Desktop.
[leetcode][984] String Without AAA or BBB : https://leetcode.com/problems/string-without-aaa-or-bbb/
class Solution {
public:
string strWithout3a3b(int A, int B) {
pair<int,int> cnt = {max(A, B), min(A,B)};
pair<char,char> alph = {(A > B) ? 'a' : 'b', (A > B) ? 'b' : 'a'};
string ans = "";
while(cnt.first > 0 && cnt.second > 0 && cnt.first > cnt.second){
ans.push_back(alph.first);
ans.push_back(alph.first);
cnt.first-=2;
ans.push_back(alph.second);
cnt.second--;
}
while(cnt.first > 0 || cnt.second > 0){
if(cnt.first > 0) {
ans.push_back(alph.first);
cnt.first--;
}
if(cnt.second > 0) {
ans.push_back(alph.second);
cnt.second--;
}
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment