The four Day 给出一个平衡字符串,将它分割成尽可能多的平衡字符串

时间:2023-03-09 08:34:09
The four Day 给出一个平衡字符串,将它分割成尽可能多的平衡字符串
"""
在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。 返回可以通过分割得到的平衡字符串的最大数量。 示例 1: 输入:s = "RLRRLLRLRL"
输出:4
解释:s 可以分割为 "RL", "RRLL", "RL", "RL", 每个子字符串中都包含相同数量的 'L' 和 'R'。 来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/split-a-string-in-balanced-strings
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
"""
"""
@author : jiyanjiao
@date :2019-10-24
""" class Solution(object):
def balancedStringSplit(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
count_L = 0
count_R = 0
flag = 0 s_list = list(s)
for s_ in s:
if s_ == 'L':
count_L += 1
else:
count_R += 1
if count_L == count_R:
count += 1
total = count_L + count_R
# 用flag来标记上次已经识别到的位置
for i in range(flag, total):
print(s_list[i], end='')
print(',', end=' ')
flag = total
return count if __name__ == '__main__':
s = Solution()
s.balancedStringSplit("RLRRLLRLRL")