LeetCode 984. String Without AAA or BBB
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
, andThe substring?
'bbb'
?does not occur in?s
.
?
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 given?a
?and?b
.
給定2個(gè)數(shù)字,然后如何組成字符串使得不能出現(xiàn)3個(gè)連續(xù)相同的字符,但是一定有這種滿足題目的組合,
所以我們就去判斷a跟b的大小,如果a>b 這時(shí)候分兩種情況,一種是a>2b的時(shí)候,這時(shí)候依次去添加aab,剩下的時(shí)候添加ab,最后a肯定還有多的,然后去添加剩下的a。
b>a的時(shí)候是相同的規(guī)則,依次去判斷即可。
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.