Longest Alternating Substring
MediumPractice library · +20 XPstringssliding window
Given a string S, find the length of the longest substring that consists of alternating characters. An alternating character substring is one where no two adjacent characters are the same.
Input
The first line contains a string S.
Output
Print a single integer representing the length of the longest alternating substring.
Constraints
1 <= |S| <= 200000, S consists of lowercase English letters.
Time limit 1 s (native code; slower languages get more)Memory 256 MB16 hidden tests
Example 1
Input
abacaba
Output
7
The longest alternating substring is 'abacaba' itself, length 7.
Example 2
Input
aaaaa
Output
1
The longest alternating substrings are 'a', 'a', 'a', 'a', 'a', each of length 1.
Hints
Similar problems
- Longest Substring Without RepeatsMedium
- Longest Prefix With No RepeatsMedium
- Longest Substring With At Most K Distinct CharactersMedium
- String with Most Unique CharactersMedium
- Minimum Window SubstringHard
- Longest Sorted SubsegmentMedium
More topics: Strings problems, Sliding Window problems
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
abacaba
Example 2 input
aaaaa