-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2_1069.java
36 lines (32 loc) · 1.21 KB
/
Q2_1069.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* NAME : PRITISH BISWAS
* CSES - REPETIONS
* LINK : https://cses.fi/problemset/task/1069
*/
import java.util.Scanner;
public class Q2_1069 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String seq = scanner.nextLine();
if (!seq.matches("[ACGT]+")) { // regex pattern-matching
// [ACGT] : expression defines a character class that matches any one character from the set and ensures that one of the characters at that position is one of the valid DNA bases
// + : ensures that there is at least one valid DNA base character
System.out.println("Invalid ! . The input should only contain 'A', 'C', 'G', and 'T'.");
return;
}
int maxRep = 1;
int curRep = 1;
for (int i = 1; i < seq.length(); i++) {
if (seq.charAt(i) == seq.charAt(i - 1)) {
curRep++;
} else {
curRep = 1;
}
if (curRep > maxRep) {
maxRep = curRep;
// maxRep updated
}
}
System.out.println(maxRep); // longest repetition
}
}