#K32727. Maximum Number of Balanced Substrings
Maximum Number of Balanced Substrings
Maximum Number of Balanced Substrings
You are given a string S consisting only of the characters L
and R
. A balanced string is defined as a substring in which the number of L
's equals the number of R
's. Your task is to determine the maximum number of balanced substrings that can be formed by partitioning S.
Formally, if we denote the number of L
's and R
's in a substring by \(\ell\) and \(r\) respectively, then a substring is balanced if:
[ \ell = r ]
It is guaranteed that the given string S is balanced overall, so at least one valid partition exists.
Input Example:
RLRRLLRLRL
Output Example:
4
inputFormat
The input consists of a single line containing the string S (only composed of the characters L
and R
). The string is provided via standard input.
outputFormat
Output a single integer denoting the maximum number of balanced substrings that can be obtained by splitting the string. The result should be printed to standard output.
## sampleRLRRLLRLRL
4