在一个 平衡字符串 中,'L' 和 'R' 字符的数量是相同的。 给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。 注意:分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。 返回可以通过分割得到的平衡字符串的 最大数量 。
只需将R,L出现的次数记下,相等时计数加一即可
class Solution(object):
def balancedStringSplit(self, s):
"""
:type s: str
:rtype: int
"""
nR=0
nL=0
n=0
for i in s:
if i=="R":
nR+=1
else:nL+=1
if nR==nL:
n+=1
return n
22.11.14