Leetcode 1026 Solution

This article provides solution to leetcode question 1026 (string-without-aaa-or-bbb)

https://leetcode.com/problems/string-without-aaa-or-bbb

Solution

class Solution: def strWithout3a3b(self, A: int, B: int) -> str: ans = []
small_cnt, small_ch = min((A, 'a'), (B, 'b')) big_cnt, big_ch = max((A, 'a'), (B, 'b'))
while small_cnt or big_cnt: if small_cnt == 0: ans.append(big_ch) big_cnt -= 1 elif big_cnt // small_cnt > 1: ans.append(big_ch) ans.append(big_ch) ans.append(small_ch) big_cnt -= 2 small_cnt -= 1 else: ans.append(big_ch) ans.append(small_ch) big_cnt -= 1 small_cnt -= 1
return "".join(ans)