LeetCode 984. String Without AAA or BBB
Given two integers a
and b
, return any string s
such that:
s
has lengtha + b
and contains exactlya
'a'
letters, and exactlyb
'b'
letters,The substring
'aaa'
does not occur ins
, andThe substring
'bbb'
does not occur ins
.
Example 1:
Input: a = 1, b = 2Output: "abb"Explanation: "abb", "bab" and "bba" are all correct answers.
Example 2:
Input: a = 4, b = 1Output: "aabaa"
Constraints:
0 <= a, b <= 100
It is guaranteed such an
s
exists for the givena
andb
.
给定2个数字,然后如何组成字符串使得不能出现3个连续相同的字符,但是一定有这种满足题目的组合,
所以我们就去判断a跟b的大小,如果a>b 这时候分两种情况,一种是a>2b的时候,这时候依次去添加aab,剩下的时候添加ab,最后a肯定还有多的,然后去添加剩下的a。
b>a的时候是相同的规则,依次去判断即可。
Runtime: 0 ms, faster than 100.00% of Java online submissions for String Without AAA or BBB.
Memory Usage: 40 MB, less than 74.00% of Java online submissions for String Without AAA or BBB.