Skip to content

Commit 2b92c00

Browse files
authored
Merge pull request #4201 from Saksham2k3s/add-solution-lc-1221
Added solution for leetcode problem 1221
2 parents 82bdff5 + 14022a2 commit 2b92c00

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
id: split-a-string-in-balanced-strings
3+
title: Split a String in balanced Strings
4+
sidebar_label: 1221. Split a String in Balanced Strings
5+
tags:
6+
- String
7+
- Greedy
8+
- Counting
9+
description: "Solution to Leetcode 1221. Split a String in Balanced Strings"
10+
---
11+
12+
## Problem Description
13+
14+
**Balanced** strings are those that have an equal quantity of `'L'` and `'R'` characters.
15+
16+
Given a balanced **string** `s`, split it into some number of substrings such that:
17+
18+
- Each substring is balanced.
19+
20+
Return the **maximum** number of balanced strings you can obtain.
21+
22+
### Examples
23+
24+
**Example 1:**
25+
26+
```
27+
Input: s = "RLRRLLRLRL"
28+
Output: 4
29+
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: s = "RLRRRLLRLL"
36+
Output: 2
37+
Explanation: s can be split into "RL", "RRRLLRLL", each substring contains same number of 'L' and 'R'.
38+
Note that s cannot be split into "RL", "RR", "RL", "LR", "LL", because the 2nd and 5th substrings are not balanced.
39+
40+
```
41+
42+
**Example 3:**
43+
44+
```
45+
Input: s = "LLLLRRRR"
46+
Output: 1
47+
Explanation: s can be split into "LLLLRRRR".
48+
49+
50+
```
51+
52+
53+
54+
### Constraints
55+
- `2 <= s.length <= 1000`
56+
- `s[i] is either 'L' or 'R'.`
57+
- `s is a balanced string.`
58+
59+
### Approach
60+
61+
The provided code solves the problem of splitting a string into the maximum number of balanced substrings. A balanced substring is defined as a substring that has an equal number of 'L' and 'R' characters. Here's a step-by-step approach to how the code works:
62+
63+
### Steps:
64+
65+
1. **Initialize Counters**:
66+
- `count` is initialized to 0 and will be used to count the number of balanced substrings.
67+
- `ch` is initialized to 0 and will be used to keep track of the balance between 'R' and 'L' characters.
68+
69+
2. **Iterate Through the String**:
70+
- The code loops through each character of the input string `s` using a `for` loop.
71+
72+
3. **Update Balance Counter**:
73+
- Inside the loop, for each character:
74+
- If the character is 'R', increment the `ch` counter.
75+
- If the character is 'L', decrement the `ch` counter.
76+
- This way, `ch` keeps track of the balance:
77+
- Positive values of `ch` indicate more 'R's than 'L's.
78+
- Negative values of `ch` indicate more 'L's than 'R's.
79+
- A `ch` value of 0 indicates an equal number of 'R' and 'L' characters up to that point.
80+
81+
4. **Check for Balanced Substring**:
82+
- After updating `ch`, the code checks if `ch` is 0.
83+
- If `ch` is 0, it means that the substring from the last balanced point (or from the start if it's the first balanced substring) to the current position is balanced.
84+
- Increment the `count` counter each time a balanced substring is found.
85+
86+
5. **Return the Result**:
87+
- After the loop completes, `count` will hold the total number of balanced substrings in the input string `s`.
88+
- The function returns the value of `count`.
89+
90+
### Example Walkthrough
91+
Let's consider the string `s = "RLRRLLRLRL"`:
92+
93+
- Initialize: `count = 0`, `ch = 0`
94+
- Loop through each character:
95+
- 'R' -> `ch = 1`
96+
- 'L' -> `ch = 0` (balanced substring found, `count = 1`)
97+
- 'R' -> `ch = 1`
98+
- 'R' -> `ch = 2`
99+
- 'L' -> `ch = 1`
100+
- 'L' -> `ch = 0` (balanced substring found, `count = 2`)
101+
- 'R' -> `ch = 1`
102+
- 'L' -> `ch = 0` (balanced substring found, `count = 3`)
103+
- 'R' -> `ch = 1`
104+
- 'L' -> `ch = 0` (balanced substring found, `count = 4`)
105+
106+
The final `count` is 4, which is the number of balanced substrings in the input string.
107+
108+
109+
### Solution
110+
111+
#### Code in Different Languages
112+
113+
#### C++
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
int balancedStringSplit(string s) {
119+
int count = 0;
120+
int ch = 0;
121+
for (char c : s) {
122+
if (c == 'R') {
123+
ch++;
124+
} else {
125+
ch--;
126+
}
127+
if (ch == 0) {
128+
count++;
129+
}
130+
}
131+
return count;
132+
}
133+
};
134+
135+
```
136+
137+
#### PYTHON
138+
139+
```python
140+
def balancedStringSplit(s: str) -> int:
141+
count = 0
142+
ch = 0
143+
for char in s:
144+
if char == 'R':
145+
ch += 1
146+
else:
147+
ch -= 1
148+
if ch == 0:
149+
count += 1
150+
return count
151+
152+
```
153+
154+
155+
156+
### Complexity Analysis
157+
158+
- **Time Complexity**: O(n)
159+
- The algorithm iterates through each character in the input string exactly once, resulting in a linear time complexity relative to the length of the string \( n \).
160+
161+
- **Space Complexity**: O(1)
162+
- The algorithm uses a fixed amount of extra space (for the `count` and `ch` variables), regardless of the input size, resulting in constant space complexity.
163+
164+
165+
### References
166+
167+
- **LeetCode Problem**: Split a String in balanced Strings

0 commit comments

Comments
 (0)