Skip to content

Commit 5219545

Browse files
authored
Add Python solution of Split a String in Balanced Strings Problem (#37)
1 parent 2cdc5d1 commit 5219545

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Diff for: Python/split-a-string-in-balanced-strings.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
Time Complexity: O(N)
4+
Space Complexity: O(1)
5+
"""
6+
7+
def balanced_string_split(self, s: str) -> int:
8+
# initialize variables
9+
L_count, R_count = 0, 0
10+
balanced_substring_count = 0
11+
12+
# parse the string
13+
for char in s:
14+
15+
# update the number of Ls and the number of Rs so far
16+
if char == 'L':
17+
L_count += 1
18+
elif char == 'R':
19+
R_count += 1
20+
21+
# if the string is balanced, increment the balanced substrings count and reset the counters
22+
if L_count == R_count:
23+
balanced_substring_count += 1
24+
L_count, R_count = 0, 0
25+
return balanced_substring_count

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
109109
| 383 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [Java](./Java/ransom-note.java) | _O(1)_ | _O(n)_ | Easy | | Character Count |
110110
| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Java](./Java/reverse-words-in-a-string.java) | _O(1)_ | _O(n)_ | Medium | | |
111111
| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Java](./Java/detect-capital-use.java) | _O(n)_ | _O(1)_ | Easy | | |
112+
| 1221 | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [Python](./Python/split-a-string-in-balanced-strings.py)| _O(n)_ | _O(1)_ | Easy | | |
113+
112114

113115
<br/>
114116
<div align="right">
@@ -329,6 +331,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
329331
| [Sachin Singh Negi](https://github.com/sachinnegi) <br> <img src="https://github.com/sachinnegi.png" width="100" height="100"> | India | Python | [Twitter](https://twitter.com/SachinSinghNe17)<br>[Leetcode](https://leetcode.com/negisachin688/)<br>[Hackerrrak](https://www.hackerrank.com/negisachin688) |
330332
| [Girish Thatte](https://github.com/girishgr8/) <br> <img src="https://github.com/girishgr8.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/girish13/) <br> [Hackerrank](https://www.hackerrank.com/procoder_13) <br> [Codechef](https://www.codechef.com/procoder_13) |
331333
| [Kevin Chittilapilly](https://github.com/KevinChittilapilly) <br> <img src="https://github.com/KevinChittilapilly.png" width="100" height="100"> | India | Java | [Leetcode](https://leetcode.com/being_kevin/) <br> [Hackerrank](https://www.hackerrank.com/ckevinvarghese11) <br> [Kaggle](https://www.kaggle.com/kevinchittilapilly) |
334+
| [Nour Grati](https://github.com/Nour-Grati) <br> <img src="https://github.com/Nour-Grati.png" width="100" height="100"> | Tunisia | Python | [Leetcode](https://leetcode.com/nourgrati/) <br> [Hackerrank](https://www.hackerrank.com/noor_grati) <br> [Twitter](https://twitter.com/GratiNour1) |
335+
332336
<br/>
333337
<div align="right">
334338
<b><a href="#algorithms">⬆️ Back to Top</a></b>

0 commit comments

Comments
 (0)