#C3032. Maximum Balanced Substrings
Maximum Balanced Substrings
Maximum Balanced Substrings
You are given a string s
consisting only of characters 'L' and 'R'. Your task is to partition the string into the maximum possible number of non-empty substrings such that each substring has an equal number of 'L' and 'R'.
Formally, for a substring sub of s
, if the number of 'L's is equal to the number of 'R's, it is considered balanced. The goal is to determine the maximum number of balanced substrings that can be obtained by splitting the string.
Note: It is guaranteed that the string can be partitioned into balanced substrings.
For example, if s = "RLRRLLRLRL"
, one valid partition is ["RL","RRLL","RL","RL"]
which gives the answer 4.
The balancing condition can be formalized as: \(\text{balance} = \#L - \#R = 0\) over each valid substring.
inputFormat
The input consists of a single line containing a string s
which only includes the characters 'L' and 'R'.
outputFormat
Output a single integer representing the maximum number of balanced substrings that can be obtained from the input string.
## sampleRLRRLLRLRL
4