diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/string_without_3a3b.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/string_without_3a3b.cpp')
-rw-r--r-- | other/string_without_3a3b.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/other/string_without_3a3b.cpp b/other/string_without_3a3b.cpp new file mode 100644 index 0000000..738d000 --- /dev/null +++ b/other/string_without_3a3b.cpp @@ -0,0 +1,38 @@ +// 984. String Without AAA or BBB +#include "leetcode.h" + +/* + * given two integers 'a' and 'b', return any string 's' such that + * - 's' has length 'a + b' and contains exactly 'a' a letters and exactly 'b' b + * letters. + * - the substring 'aaa' does not occur in 's' and + * - the substring 'bbb' does not occur in 's' + */ + +class Solution { +public: + string strWithout3a3b(int a, int b) { + string ans = ""; + int cnta = 0, cntb = 0, total = a + b; + for (int i = 0; i < total; i++) { + if ((b >= a && cntb < 2) || (cnta == 2 && b)) { + ans += 'b'; + b--; + cntb++; + cnta = 0; + } else if ((a >= b && cnta < 2) || (cntb == 2 && a)) { + ans += 'a'; + a--; + cnta++; + cntb = 0; + } + } + return ans; + } +}; + +int main() { + Solution obj; + cout << obj.strWithout3a3b(1, 2) << endl; // expect: "abb" + cout << obj.strWithout3a3b(4, 1) << endl; // expect: "aabaa" +} |