-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1221.py
More file actions
44 lines (42 loc) · 1.26 KB
/
1221.py
File metadata and controls
44 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/4/20 17:48
# @Author : Fhh
# @File : 1221.py
# Good good study,day day up!
"""在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。
给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
返回可以通过分割得到的平衡字符串的最大数量。
示例 1:
输入:s = "RLRRLLRLRL"
输出:4
解释:s 可以分割为 "RL", "RRLL", "RL", "RL", 每个子字符串中都包含相同数量的 'L' 和 'R'。
示例 2:
输入:s = "RLLLLRRRLR"
输出:3
解释:s 可以分割为 "RL", "LLLRRR", "LR", 每个子字符串中都包含相同数量的 'L' 和 'R'。
示例 3:
输入:s = "LLLLRRRR"
输出:1
解释:s 只能保持原样 "LLLLRRRR".
提示:
1 <= s.length <= 1000
s[i] = 'L' 或 'R'
"""
class Solution:
def balancedStringSplit(self, s: str) -> int:
cntl = 0
cntr = 0
cnt = 0
for i in range(len(s)):
if cntl == cntr and cntl != 0:
cnt += 1
cntl = 0
cntr = 0
if s[i] == 'R':
cntr += 1
else:
cntl += 1
return cnt+1
s=Solution()
print(s.balancedStringSplit("RLRRLLRLRLLLRRRL"))