Back to Solutions

Repetitions

Explanation

Here, we are provided a string and we need to find the longest substring consisting of only a single type of character.

This can be done using a greedy algorithm, by adding 1 to count if we receive the same character as the last one, or resetting it to 1, if we don't. Then we result out the maximum value of count.

Code

s = input()

count = 0
max_count = 0
last = s[0]

for ch in s:
    count = count + 1 if ch == last else 1

    last = ch
    max_count = max(max_count, count)

print(max_count)