AlternatingString
SRM 703 · 2016-12-04 · by cgy4ever
SRM 703 · 2016-12-04 · by cgy4ever · Brute Force
Problem Statement
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 aString 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
Constraints
- s will contain between 1 and 50 characters, inclusive.
- Each character in s will be '0' or '1'.
Examples
0)
"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.
1)
"1010101" Returns: 7
The string s itself is an alternating string.
2)
"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.
3)
"1011011110101010010101" Returns: 8
4)
"0" Returns: 1
Submissions are judged against all 114 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class AlternatingString with a public method int maxLength(string s) · 114 test cases · 2 s / 256 MB per case