Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5cacebc

Browse files
committed
Have implemented the program to find the count of the balanced strings.
1 parent 604face commit 5cacebc

File tree

2 files changed

+85
-21
lines changed

2 files changed

+85
-21
lines changed

.idea/workspace.xml

Lines changed: 24 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Split a String in Balanced Strings
3+
https://leetcode.com/problems/split-a-string-in-balanced-strings/
4+
5+
Balanced strings are those that have an equal quantity of 'L' and 'R' characters.
6+
7+
Given a balanced string s, split it into some number of substrings such that:
8+
9+
Each substring is balanced.
10+
Return the maximum number of balanced strings you can obtain.
11+
12+
Example 1:
13+
Input: s = "RLRRLLRLRL"
14+
Output: 4
15+
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'.
16+
17+
Example 2:
18+
Input: s = "RLRRRLLRLL"
19+
Output: 2
20+
Explanation: s can be split into "RL", "RRRLLRLL", each substring contains same number of 'L' and 'R'.
21+
Note that s cannot be split into "RL", "RR", "RL", "LR", "LL", because the 2nd and 5th substrings are not balanced.
22+
23+
Example 3:
24+
Input: s = "LLLLRRRR"
25+
Output: 1
26+
Explanation: s can be split into "LLLLRRRR".
27+
28+
Constraints:
29+
2 <= s.length <= 1000
30+
s[i] is either 'L' or 'R'.
31+
s is a balanced string.
32+
*/
33+
package com.raj;
34+
35+
public class SplitAStringInBalancedStrings {
36+
public static void main(String[] args) {
37+
// Initialization.
38+
String s = "RLRRLLRLRL";
39+
int RCounter = 0;
40+
int LCounter = 0;
41+
int ans = 0;
42+
43+
44+
// Logic.
45+
for (int i = 0; i < s.length(); i++) {
46+
if (s.charAt(i) == 'R') {
47+
RCounter++;
48+
} else {
49+
LCounter++;
50+
}
51+
if (RCounter == LCounter) {
52+
ans++;
53+
LCounter = 0;
54+
RCounter = 0;
55+
}
56+
}
57+
58+
// Display the result.
59+
System.out.println("The balanced string count is: " + ans);
60+
}
61+
}

0 commit comments

Comments
 (0)