【leetcode】1221. Split a String in Balanced Strings
题目如下:
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
Given a balanced string
s
split it in the maximum amount of balanced strings.Return the maximum amount of splitted balanced strings.
Example 1:
Input: s = "RLRRLLRLRL"
Output: 4
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.Example 2:
Input: s = "RLLLLRRRLR"
Output: 3
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of 'L' and 'R'.Example 3:
Input: s = "LLLLRRRR"
Output: 1
Explanation: s can be split into "LLLLRRRR".Constraints:
1 <= s.length <= 1000
s[i] = 'L' or 'R'
解题思路:从头开始遍历s,分别计算L和R的个数。如果两者相等,表示可以分成一组。
代码如下:
class Solution(object):
def balancedStringSplit(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
r_count = 0
l_count = 0
for i in s:
if i == 'R':r_count+=1
else:l_count += 1
if r_count == l_count and r_count != 0:
res += 1
r_count = l_count = 0
return res
【leetcode】1221. Split a String in Balanced Strings的更多相关文章
- 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...
- [LeetCode]1221. Split a String in Balanced Strings
Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced strin ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【leetcode】1234. Replace the Substring for Balanced String
题目如下: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string i ...
- 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】725. Split Linked List in Parts
Given a (singly) linked list with head node root, write a function to split the linked list into k c ...
- 【LeetCode】761. Special Binary String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...
随机推荐
- Tlbimp 生成c#互操作com组件dll;Tlbimp 生成.net互操作com组件dll
Tlbimp 生成c#互操作com组件dll vs开发人员命令提示>进入到com组件目录>Tlbimp com.dll
- LeetCode.1002-寻找共有字符(Find Common Characters)
这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...
- Scratch少儿编程系列:(十)系列总结及后续计划
一.系列文章的来由 本篇为该系列文章的一个简单总结, 从初次接触Scratch开始,在写本系列文章过程中,一边读书,一边通过例子做练习. 技术实现,对于我跟人来说,没有什么难度. 我相信,对于一个初次 ...
- linux中权限对文件和目录的影响?
######### rwx 权限对文件和目录的含义 ############ 代表字符 权限 对文件的含义 对 目录 ...
- 【VS开发】【图像处理】 bayer, yuv, RGB转换方法
因为我的STVxxx USB camera输出格式是bayer格式,手头上只有YUVTOOLS这个查看工具,没法验证STVxxx在开发板上是否正常工作. 网上找了很久也没找到格式转换工具,最后放弃了, ...
- k8s-kubernetes-configmap存储
存储 configMap configMap描述信息 ConfigMap功能在Kubernetes1.2版本中引入,许多应用程序会从配置文件.命令行参数或环境变量中读取配置信息. ConfigMap ...
- 学习Linux第一周记
2019/11/25 服务器硬件详述1) CPU 作用:运算/控制 关注信息 :路数 服务器中CPU的颗数 一般有 (单路 双路 ...
- CDH日常运维
1/ 作业有问题: 查log,没log再跑一次查log. 如果没有log的情况,比如hiveserver2挂了,查strr. 2/ 查集群名字 #看hdfs集群的名字,在cdh的hdfs配置中查:na ...
- java来接收邮件并解析邮件正文中的表格
这里是实际需求中的一个DEMO 有一部分内容进行了注释和处理,参考需要修改成自己的实际参数.另这个是对于实际一个场景的案例并不是通用解决的工具类. import org.jsoup.Jsoup; im ...
- 【6.12校内test】T2 子集
这道题大概是这三道题里最简单的啦 但这阻止不了我废的脚步 [问题描述] 对于 n=4 时,对应的集合 s={4,3,2,1},他的非空子集有 15 个依次如下: {1} {2} {1,2} {3} { ...