forked from AishwaryaBarai/Leetcode-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1221. Split a String in Balanced Strings
42 lines (35 loc) · 1.11 KB
/
1221. Split a String in Balanced Strings
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
naive:
class Solution:
def balancedStringSplit(self, s: str) -> int:
count_R = 0
count_L = 0
final_count = 0
for i in s:
if i == 'R':
count_R +=1
if count_R == count_L and count_R != 0 and count_L !=0:
final_count +=1
count_R = 0
count_L = 0
else:
count_L +=1
if count_R == count_L and count_R != 0 and count_L !=0:
final_count +=1
count_R = 0
count_L = 0
return final_count
class Solution:
def balancedStringSplit(self, s: str) -> int:
count_R = 0
count_L = 0
final_count = 0
for i in s:
if i == 'R':
count_R +=1
elif i == 'L':
count_L +=1
if count_R == count_L and count_R != 0 and count_L !=0:
final_count +=1
count_R = 0
count_L = 0
return final_count