欢迎光临散文网 会员登陆 & 注册

LeetCode 984. String Without AAA or BBB

2023-05-21 09:29 作者:您是打尖儿还是住店呢  | 我要投稿

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.

 

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个数字,然后如何组成字符串使得不能出现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.


LeetCode 984. String Without AAA or BBB的评论 (共 条)

分享到微博请遵守国家法律