Created
February 14, 2019 13:44
-
-
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/
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: | |
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