Problem Statement
A string of zeros and ones is called an alternating string if no two adjacent characters are the same.
Examples of alternating strings: "1", "10101", "0101010101".
You are given a String s. Each character of s is a '0' or a '1'. Please find the longest contiguous substring of s that is an alternating string. Return the length of that substring.
You are given a String s. Each character of s is a '0' or a '1'. Please find the longest contiguous substring of s that is an alternating string. Return the length of that substring.
Definition
- Class:
- AlternatingString
- Method:
- maxLength
- Parameters:
- String
- Returns:
- int
- Method signature:
- int maxLength(String s)
- (be sure your method is public)
Constraints
- s will contain between 1 and 50 characters, inclusive.
- Each character in s will be '0' or '1'.
Examples
"111101111"
Returns: 3
Among all substrings, there are 5 different alternating strings: "1", "0", "10", "01", "101". The one with maximal length is "101" and the length is 3."1010101"
Returns: 7
The string s itself is an alternating string."000011110000"
Returns: 2
Note that a substring must be contiguous. The longest alternating substrings of this s are "01" and "10". The string "010" is not a substring of this s."1011011110101010010101"
Returns: 8
"0"
Returns: 1