【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- ...
随机推荐
- <数据结构系列3>队列的实现与变形(循环队列)
数据结构第三课了,今天我们再介绍一种很常见的线性表——队列 就像它的名字,队列这种数据结构就如同生活中的排队一样,队首出队,队尾进队.以下一段是百度百科中对队列的解释: 队列是一种特殊的线性表,特殊之 ...
- H3C版本升级
H3C S5500和S3100都可以通过TFTP方式进行升级,1.设置交换机的vlan1的ip地址,如10.10.10.2/242.设置电脑和交换机连接的ip地址,如10.10.10.1/243.在交 ...
- aliyun搭博客从零到一
一.基础环境 lnmp 1台负载均衡SLB 2台ECS 1台 RDS 二.lnmp搭建 1.#配置nginx的yum仓库 2.#yum install -y nginx ...
- 线性模型-线性回归、Logistic分类
线性模型是机器学习中最简单的,最基础的模型结果,常常被应用于分类.回归等学习任务中. 回归和分类区别: 回归:预测值是一个连续的实数: 分类:预测值是离散的类别数据. 1. 线性模型做回归任务 ...
- 【Qt开发】布局控件之间的间距设置
void QLayout::setContentsMargins ( int left, int top, int right, int bottom ) Sets the left, top, ri ...
- linux 更改文件权限命令 chmod
chmod -change file mode bits :更改文件权限 chmod是用来改变文件或者目录权限的命令,但只有文件的属主和超级用户(root)才有这种权限. 更改文件权限的2种方式: 一 ...
- PTA(Basic Level)1061.判断题
判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 M,分别是学生人数和判断题数量.第二行给出 M ...
- 滑雪(dp或记忆化搜索)
题意:给你一个二维数组,求最长的递减路线的长度,只能向四个方向延伸. 解法1.dp[i][j]以i.j结尾的最长路线长度.边界:每个数初值为1, 转移:从四周向i.j转移,if(a[i][j]> ...
- scrapy之盗墓笔记三级页面爬取
#今日目标 **scrapy之盗墓笔记三级页面爬取** 今天要爬取的是盗墓笔记小说,由分析该小说的主要内容在三级页面里,故需要我们 一一解析 *代码实现* daomu.py ``` import sc ...
- Java Web开发技术教程入门-初识动态网页
这段时间学校搞了一个"阅战阅勇"的阅读活动,奖品还是挺丰富的~于是,奔着这些奖品,我去图书馆借了这本<Java Web开发技术教程>.一是为了那些丰富的奖品,二是为了回 ...