题目如下:

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的更多相关文章

  1. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  2. [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 ...

  3. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  4. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  5. 【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 ...

  6. 【LeetCode】725. Split Linked List in Parts 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【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 ...

  9. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

随机推荐

  1. spring ehcache 缓存框架

    一.简介 Ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache ...

  2. python学习之socket&黏包

    7.4 socket ​ [重要] 避免学习各层的接口,以及协议的使用, socket已经封装好了所有的接口,直接使用这些接口或者方法即可,方便快捷,提升开发效率. socket在python中就是一 ...

  3. H3C版本升级

    H3C S5500和S3100都可以通过TFTP方式进行升级,1.设置交换机的vlan1的ip地址,如10.10.10.2/242.设置电脑和交换机连接的ip地址,如10.10.10.1/243.在交 ...

  4. 基于LVM(逻辑卷管理)的快照原理

    一.为甚么不管多大的逻辑卷进行备份,快照都会在几秒之内完成? 快照在拍摄的一瞬间,系统会记录那个时间点逻辑卷的状态.数据等,此时拍下的快照相当于一张白纸.如图所示 快照做好后,随着时间的推移,源卷里的 ...

  5. Maven使用基础

    (转)https://my.oschina.net/xiaomaoandhong/blog/104045 基于 约定优于配置(Convention Over Configuration)的原则,无特殊 ...

  6. 项目附 - 云盘项目-分析echo.c

    分析FastCGI源码目录下example中echo.c代码: /* * echo.c -- * * Produce a page containing all FastCGI inputs * * ...

  7. [转贴]linux lsof命令详解

    linux lsof命令详解 https://www.cnblogs.com/sparkbj/p/7161669.html 简介 lsof(list open files)是一个列出当前系统打开文件的 ...

  8. 小记---------kafka理论及命令行操作

    kafka-0.10.1.X版本之前: auto.offset.reset 的值为smallest,和,largest.(offest保存在zk中)   kafka-0.10.1.X版本之后: aut ...

  9. Codeforces893F_Subtree Minimum Query

    题意 给定一棵树和根,每个点有点权,强制在线询问\(x\)子树里离\(x\)距离不超过\(k\)的最小点权. 分析 权值线段树合并的套路题,dfs,以深度作为下标,点权作为值,对每个点建立一颗权值线段 ...

  10. C#获取局域网主机

    C#获取局域网主机 最近在做一个使用MSRDPClient来实现远程桌面功能,需要先判断一下该局域网主机是否在线,所以就需要获取一遍局域网主机. 首先获取本地IP地址,这里需要注意的是,要排除掉虚拟机 ...