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
; The substring ‘bbb‘
does not occur in S
. Example 1:
input: A = 1,B = 2 Output: "abb" Explanation: "abb","bab" and "bba" are all correct answers.
Example 2:
input: A = 4,B = 1 Output: "aabaa"
Note:
0 <= A <= 100
0 <= B <= 100
It is guaranteed such an S
exists for the given A
and B
. 给定两个整数 A
和 B
,返回任意字符串 S
,要求满足:
S
的长度为 A + B
,且正好包含 A
个 ‘a‘
字母与 B
个 ‘b‘
字母; 子串 ‘aaa‘
没有出现在 S
中; 子串 ‘bbb‘
没有出现在 S
中。 示例 1:
输入:A = 1,B = 2输出:"abb"解释:"abb","bab" 和 "bba" 都是正确答案。
示例 2:
输入:A = 4,B = 1输出:"aabaa"
提示:
0 <= A <= 100
0 <= B <= 100
对于给定的 A
和 B
,保证存在满足要求的 S
。 12ms
1 class Solution { 2 func strWithout3a3b(_ A: Int,_ B: Int) -> String { 3 var A = A 4 var B = B 5 var ret:[Character] = [Character](repeating:" ",count:A+B) 6 for i in 0..<ret.count 7 { 8 if i >= 2 && ret[i-1] == ret[i-2] 9 {10 if ret[i-1] == "a"11 {12 ret[i] = "b"13 B -= 114 }15 else16 {17 ret[i] = "a"18 A -= 119 }20 }21 else22 {23 if A > B24 {25 ret[i] = "a"26 A -= 127 }28 else29 {30 ret[i] = "b"31 B -= 132 }33 } 34 }35 return String(ret.reversed())36 }37 }总结
以上是内存溢出为你收集整理的[Swift Weekly Contest 121]LeetCode984. 不含 AAA 或 BBB 的字符串 | String Without AAA or BBB全部内容,希望文章能够帮你解决[Swift Weekly Contest 121]LeetCode984. 不含 AAA 或 BBB 的字符串 | String Without AAA or BBB所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)