#C10923. Longest Substring with Equal Number of 'a's and 'b's
Longest Substring with Equal Number of 'a's and 'b's
Longest Substring with Equal Number of 'a's and 'b's
Given a string s
consisting solely of the characters 'a'
and 'b'
, your task is to find the length of the longest contiguous substring that contains an equal number of 'a'
s and 'b'
s.
More formally, let \( S \) be the given string. You need to determine the maximum length \( L \) for which there exists a substring \( S[i \ldots j] \) such that:
[ #a(S[i \ldots j]) = #b(S[i \ldots j]) ]
Input Constraints: The input string will contain at least one character and will only consist of the letters 'a'
and 'b'
.
Your algorithm should be efficient to handle large strings.
inputFormat
The input consists of a single line containing a non-empty string s
made up of only 'a'
and 'b'
characters.
outputFormat
Output a single integer which is the length of the longest contiguous substring of s
that contains an equal number of 'a'
s and 'b'
s.
abba
4