【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 ...
随机推荐
- ajax使用jsonp请求方式
/* //简写形式,效果相同 $.getJSON("http://app.example.com/base/json.do?sid=1494&busiId=101&jsonp ...
- 【优化】Mysql字段尽可能用NOT NULL
下面咱们要聊的是 MySQL 里的 null,在大量的 MySQL 优化文章和书籍里都提到了字段尽可能用NOT NULL,而不是NULL,除非特殊情况.但却都只给结论不说明原因,犹如鸡汤不给勺子一样, ...
- 数据流:DataOutputStream与DataInputStream的使用
看这两个类的名字就不难猜测出它们的类关系图. DataOutputStream: 主要是一些writeXxx()操作,写出, 相当于序列化 DataInputStream: 主要是一些readXxx( ...
- Kafka数据如何同步至MaxCompute之实践讲解
摘要:本次分享主要介绍Kafka产品的原理和使用方式,以及同步数据到MaxCompute的参数介绍.独享集成资源组与自定义资源组的使用背景和配置方式.Kafka同步数据到MaxCompute的开发到生 ...
- cf 811c Vladik and Memorable Trip
原题链接:http://codeforces.com/contest/811/problem/C 题意:将数组中的连续数字连成若干个“线段”(或者不连),其实就是区间.区间必须满足对于其中的任意数字, ...
- Map-Amap:货运解决方案
ylbtech-Map-Amap:货运解决方案 1.返回顶部 1. http://lbs.amap.com/smart/truck/ 2. 2.返回顶部 1. 2. 3.返回顶部 4.返回顶部 ...
- .net 运行原理
刚学习那会,感觉.net运行原理是很复杂的,也去了解过相关的东西,但是很晦涩,难于理解.感觉有些难了,也就放弃了解了.今天回头想想,也是当时有些毛躁了,不管怎么说,时至今日是有些明白运行原理. 从头开 ...
- 简单DP入门(二) 最长上升子序列及其优化
最长上升子序列解决问题: 有N个数,求出它最长的上升子序列并输出长度. 在题里不会讲的这么直白,这个算法往往会与其他的算法混在一起使用. 在这篇文章中不会出现其他的例题,为了让大家更好的理解,我只会对 ...
- Android深度探索-卷1第四章心得体会
这一章的和三章的git用法有联系,so,吧上一章的git基本用法搞好了再来,具体的方法就是看书上网查,这里就不做详细步骤介绍了.这章就有点意思了,是源码的下载和编译,有能看的,能自己鼓捣的,本章介绍的 ...
- linux目录及文件命令学习
学习Linux 目录操作 1.pwd 可以看当前目录路径 2.cd change directory 改变目录,切换目录 cd / 进入跟目录 cd ..返回上级目录 cd 进入用户主目录 cd .. ...