[CSES] Repetitions
Repetitions
Task:
You are given a DNA sequence: a string consisting of characters A, C, G, and T. Your task is to find the longest repetition in the sequence. This is a maximum-length substring containing only one type of character.
Input
The only input line contains a string of $n$ characters.
Output
Print one integer: the length of the longest repetition.
Constraints
- $1 \le n \le 10^6$
Input:
ATTCGGGA
Output:
3
Solutions:
Intuition:Just checking for the longest substring with the same character.
Implementation: 1dna=input()
2def repetitions(dna):
3 res,current=1,1
4 for i in range(1,len(dna)):
5 if dna[i-1]==dna[i]:
6 current+=1
7 else:
8 current=1
9 continue
10 res=max(current,res)
11 return res
12print(repetitions(dna))
13
Analysis:
Time complexity: It's straightforward that the Time complexity is O(N).
Space complexity: O(1).