【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 ...
随机推荐
- 如何在Sketch 54 for mac创建符号?
Sketch 54 for mac是Mac系统平台上一个出色的数字设计绘图软件,小巧而不失功能齐全, 简约而不失强大!从最初的想法到最终的艺术品,可以通过Sketch 54 for mac来实现!现本 ...
- HTTP协议-get请求与post请求的区别
区别: 参数:get通过url进行传递:post放在request body中 长度:get请求在url的长度是有限制的:而post没有(其实这个限制是来自浏览器和web服务器对url的限制,并不是h ...
- boost container
Boost Container provides additional advantages: (1) The interface of the containers resemble those o ...
- [USACO09DEC] Video Game Troubles
背包DP:有依赖的背包问题 #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...
- [CSP-S模拟测试]:Miner(欧拉路)
题目背景 $And\ the\ universe\ said\ you\ are\ the\ daylight \\ And\ the\ universe\ said\ you\ are\ the\ ...
- 前端每日实战:29# 视频演示如何不用 transition 和 animation 也能做网页动画
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/BxbQJj 可交互视频教程 此视频 ...
- 【前端技术】一篇文章搞掂:微信小程序
实战: 1.[openId]获取openId 有如下几种方法: 通过wx.login()获取临时登录凭证 code,然后通过code2session获取openId wx.login():https: ...
- meta标签 使用说明(http-equiv、refresh、seo)
meta标签 使用说明(http-equiv.refresh.seo) meta标签,是在head标签里面,一般用做页面描述的.它的内容,用来描述页面一些信息的,如类型.编码.作者.简介等!虽然,它不 ...
- 网络安全随笔 - Linux的netstat查看端口 0.0.0.0与127.0.0.1的区别
linux运维都需要对端口开放查看 netstat 就是对端口信息的查看 # netstat -nltp p 查看端口挂的程序 [root@iz2ze5is23zeo1ipvn65aiz ~]# n ...
- token是什么?和session什么区别,怎么用
对于初学者来说,对Token和Session的使用难免会限于困境,开发过程中知道有这个东西,但却不知道为什么要用他?更不知道其原理,今天我就带大家一起分析分析这东西. 一.我们先解释一下他的含义: 1 ...