【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 ...
随机推荐
- python3用pygame实现播放音乐文件
import pygameimport time #导入音乐文件file = r'C:\1.wav'pygame.mixer.init()track = pygame.mixer.music.load ...
- springMVC的controller更改了,如何不重启,而自动刷新的解决办法(亲测,一招解决)
Tomcat con/ service.xml 配置如下一行代码: <Context reloadable="true"/> </Host> 然后以de ...
- Apache + Tomcat 集群的配置
公司最近新接了一个项目,客户要求WEB SERVER 必须是APACHE,,由于之前的项目一直都是NGINX,,无奈啊,,由于对方的IT又是在三哥的过度,经过一番的争执只能顺应三哥三姐的要求..
- html常用代码
<marquee width="70%" scrollamount="2">大家好</marquee> // 大家好 字符从左到右 ...
- ES6 Generator使用
// generator介绍: function* hello() { console.log("hello world") } hello();//没有执行 // 直接调用hel ...
- Oralce常用系统函数
dual:Oracle系统内部提供的一个用于实现临时数据计算的特殊表,它只有一个列DUMMY (VARCHAR2(1)) 字符类函数: concat--连接字符串 initcap--每个单词首字母大写 ...
- 九条命令检查Linux服务器性能
一.uptime命令 这个命令可以快速查看机器的负载情况.在Linux系统中,这些数据表示等待CPU资源的进程和阻塞在不可中断IO进程(进程状态为D)的数量.这些数据可以让我们对系统资源使用有一个宏观 ...
- Html5 学习笔记 【PC固定布局】 实战4 footer 区域
最终效果图: Html代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta chars ...
- T1387:搭配购买(buy)
[题目描述] Joe觉得云朵很美,决定去山上的商店买一些云朵.商店里有n朵云,云朵被编号为1,2,…...,n,并且每朵云都有一个价值.但是商店老板跟他说,一些云朵要搭配来买才好,所以买一朵云则与这朵 ...
- QTP read or write XML file
'strNodePath = "/soapenv:Envelope/soapenv:Body/getProductsResponse/transaction/queryProducts/qu ...