【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
ssplit 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 <= 1000s[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- ...
随机推荐
- Centos7 安装可视化界面
yum group list Output: Loaded plugins: fastestmirror There is no installed groups file. Maybe run: y ...
- 基于Opencv的自适应中值滤波函数selfAdaptiveMedianBlur()
7.3.3 自适应滤波器 自适应中值滤波器 对于7.3.2节所讨论的中值滤波器,只要脉冲噪声的空间密度不大,性能还是可以的(根据经验需Pa和Pb小于0.2).本节将证明,自适应中值滤波器可以处理更大概 ...
- 【转载】GitHub 标星 1.2w+,超全 Python 常用代码合集,值得收藏!
本文转自逆袭的二胖,作者二胖 今天给大家介绍一个由一个国外小哥用好几年时间维护的 Python 代码合集.简单来说就是,这个程序员小哥在几年前开始保存自己写过的 Python 代码,同时把一些自己比较 ...
- python 有参数的装饰器
怎么样为装饰器加参数 import time current_user = {'user':None} def auth(engine = "file") def deco(fun ...
- springboot基于方法级别注解事务的多数据源切换问题
springBoot多数据源配置 配置读数据源 @Component @ConfigurationProperties(prefix = "jdbc.read") @Propert ...
- [百度百科]PCI-E的速度
在早期开发中,PCIe最初被称为HSI(用于高速互连),并在最终确定其PCI-SIG名称PCI Express之前,将其名称更改为3GIO(第三代I / O). 名为阿拉帕霍工作组(AWG)的技术工作 ...
- 思考--mysql 分库分表的思考
查询不在分库键上怎么办,扫描所有库?由于分库了,每个库扫描很快?所以比单个表的扫描肯定快,可以这样理解吗. 多表jion怎么弄,把内层表发给每个分库吗? citus,tidb 都有这些问题,citus ...
- mysql自增主键字段重排
不带外键模式的 mysql 自增主键字段重排 1.备份表结构 create table table_bak like table_name; 2.备份表数据 insert into table_bak ...
- python计算1~100的和,1~100奇数的和,1~100偶数的和,一条代码求1~100的和
1.计算1~100的数之和----for循环实现1~100的和 sum1 = ,): sum1 = sum1 + i i += print(f"1-100之间的和是:{sum1}" ...
- Win7(64位)下安装Anaconda+Tensorflow(CPU)
一.安装Python 3.5 下载Anaconda网址:https://www.anaconda.com/download/ 安装:Anaconda3-4.2.0-Windows-x86_64.exe ...