【leetcode】984. String Without AAA or BBB
题目如下:
Given two integers
AandB, return any stringSsuch that:
Shas lengthA + Band contains exactlyA'a'letters, and exactlyB'b'letters;- The substring
'aaa'does not occur inS;- The substring
'bbb'does not occur inS.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 <= 1000 <= B <= 100- It is guaranteed such an
Sexists for the givenAandB.
解题思路:由于aaa/bbb是不允许的,因此Output必定只能由a,aa,b,bb四种组合。假设这四种组合的出现次数分别是i,j,k,l。那么应该满足 i+2*j = A,k+2*l = B,abs((i+j) - (k+l)) <= 1 (两者的差必须小于等于1,否则可能会出现aaa/bbb的情况)。 接下来只要穷举,找出其中一个符合条件的组合即可。
代码如下:
class Solution(object):
def strWithout3a3b(self, A, B):
"""
:type A: int
:type B: int
:rtype: str
"""
a_1 = A
a_2 = A/2
b_1 = B
b_2 = B/2
def calc(a_1,a_2,b_1,b_2):
for i in range(a_1+1):
for j in range(a_2+1):
if i + 2*j != A:
continue
for k in range(b_1+1):
for l in range(b_2+1):
if k + 2*l != B:
continue
if abs(i+j-k-l) <= 1:
return i,j,k,l
i,j,k,l = calc(a_1,a_2,b_1,b_2)
res = ''
next = 'a' if (i+j) >= (k+l) else 'b'
while i + j + k + l > 0:
if next == 'a':
if i > 0:
res += next
i -= 1
else:
res += next*2
j -= 1
next = 'b'
else:
if k > 0:
res += next
k -= 1
else:
res += next*2
l -= 1
next = 'a'
return res
【leetcode】984. String Without AAA or BBB的更多相关文章
- 【LeetCode】984. String Without AAA or BBB 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- LC 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 ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【leetcode】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- 【leetcode】 Interleaving String (hard)
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode】 Scramble String (hard)★
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 【leetcode】Interleaving String
Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
随机推荐
- 【leetcode】436. Find Right Interval
题目如下: 解题思路:题目要求的是对于任意一个区间i,要找出一个区间j,使得j的起点最接近i的终点.既然这样,我们可以把所有区间的终点组成一个列表,并按大小排序,使用二分查找就可以快速找到j区间.注意 ...
- javaScript的关键字与保留字
JavaScript 关键字: break case catch continue default delete do else finally for function if in instance ...
- [转]Windows 10 无法访问共享的解决办法大全
本文前面介绍 Windows 10 操作系统无法访问其他电脑的共享文件夹,而其他电脑访问该共享可以访问的解决办法. 简单点说就是,你的操作系统是 Win10 ,你访问不了某台电脑的共享,但是别人可以. ...
- tomcat启动一闪而过处理
进入tomcat安装目录(解压目录)下的bin目录,比如D:\Tomcat1\apache-tomcat-7.0.810\bin,打开startup.bat文件,在最上面加上下面两句: SET JAV ...
- sql 2008查看进程情况和对应语句,检查死锁进程
---------------------------------进程情况1----------------------- --得到SPID if object_id('tempdb..#info') ...
- 10.17 linux 文件权限
文件权限模拟练习 [root@wen ~]# groupadd incahome[root@wen ~]# usersdd oldboy -g incahome-bash: usersdd: comm ...
- JS当中的无限分类递归树
列表转换成树形结构方法定义: //javascript 树形结构 function toTree(data) { // 删除 所有 children,以防止多次调用 data.forEach(func ...
- LintCode之合并排序数组
题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...
- jQuery-resize和scroll的性能优化
## 下面是进行测试和研究怎么实现的用的 Document 改变页面大小试试 Document 滚动滚动条试试
- nlp学习笔记
https://mp.weixin.qq.com/s/-w4gENfBt2gKOPvghenw9w