题目如下:

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. java里null强转为某个类会报错吗?

    1.定义一个User类如下: /** * @author lizhibiao * @date 2018/11/27 17:21 */public class User{ private String ...

  2. matlab2012a过期问题解决办法(转载)

    转载:http://blog.sina.com.cn/s/blog_4a46812b0102x694.html   以前安装过Matlab2013a等高版本,发现自己win7 系统每次重启后,Matl ...

  3. C++类大小的计算

    这里记录一下怎么计算类对象的大小. 大概总结下,类的大小需要考虑以下内容: 非静态成员变量大小 数据对齐到多少位 有无虚函数(即需不需要指向虚函数表的指针,如果考虑继承的情况,则还需要看继承了多少个指 ...

  4. python 并发编程 多线程 互斥锁

    互斥锁 并行变成串行,牺牲效率 保证数据安全,实现局部串行 保护不同的数据,应该加不同的锁 现在一个进程 可以有多个线程 所有线程都共享进程的地址空间 实现数据共享 共享带来问题就会出现竞争 竞争就会 ...

  5. PMP几种说明书

    信息系统的生命周期包括5个阶段:系统规划(可行性分析与项目开发计划).系统分析(需求分析).系统设计(概要设计.详细设计).系统实施(编码.测试).运行维护等阶段. 系统规划阶段:初步调查组织环境目标 ...

  6. TP5报错Fatal error: require(): Failed opening required '/home/www/xx/public/../thinkphp/start.php

    https://jingyan.baidu.com/article/afd8f4deb784fe34e386e97b.html https://www.cnblogs.com/300js/p/9224 ...

  7. jira:恢复数据:AO_187CCC_SIDEBAR_LINK

    JIRA 恢复数据时报错 ,关键词是找不到 AO_187CCC_SIDEBAR_LINK. 经网上查为 mysql connect jar 包 的版本过高所致. 降低版本后,成功导入数据.

  8. 【6.18校内test】T1多项式输出

    日常题前废话: 首先so amazing 的一件事,因为在洛谷上立下了的flag,然后这次考试前两道题都是刚刚做过不久的题emmm(相当于白送200吗qwq,但是这阻挡不了我第三题不会的脚步qwq) ...

  9. Microsoft BarCode Control 16.0属性

    Labview(2018)可通过Active调用Microsoft BarCode Control 16.0来生成条形码, 参考资料如下: 生成效果: 二维码: 条形码: 执行程序发现修改线条宽度不影 ...

  10. Python笔记(二)

    python笔记 函数式编程 函数 函数是Python内建支持一种封装(将大段代码拆成函数) 通过函数的调用,可以将复制的任务分解. 函数式编程(Functional Programming) 计算机 ...