-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckCharacters.java
More file actions
40 lines (30 loc) · 1017 Bytes
/
Copy pathCheckCharacters.java
File metadata and controls
40 lines (30 loc) · 1017 Bytes
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
37
38
39
40
package lab_assignment_1;
import java.util.Scanner;
public class CheckCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input: String S
String S = scanner.nextLine();
int countA = 0, countB = 0, countC = 0;
int checkedCharacters = 0;
// Check characters from the left until all A, B, and C appear at least once
for (char ch : S.toCharArray()) {
checkedCharacters++;
// Update counts for A, B, and C
if (ch == 'A') {
countA++;
} else if (ch == 'B') {
countB++;
} else if (ch == 'C') {
countC++;
}
// Check if all A, B, and C have appeared at least once
if (countA > 0 && countB > 0 && countC > 0) {
break;
}
}
// Output the result
System.out.println(checkedCharacters);
scanner.close();
}
}