-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAlternatingCharacters.java
More file actions
30 lines (26 loc) · 869 Bytes
/
Copy pathAlternatingCharacters.java
File metadata and controls
30 lines (26 loc) · 869 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
// https://www.hackerrank.com/challenges/alternating-characters/problem
package strings;
import java.util.Scanner;
public class AlternatingCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
while (queries-- > 0) {
String string = scanner.next();
System.out.println(minimumDeletions(string));
}
}
private static int minimumDeletions(String string) {
char current = string.charAt(0);
int deletions = 0;
for (int index = 1 ; index < string.length() ; index++) {
char character = string.charAt(index);
if (current == character) {
deletions++;
} else {
current = character;
}
}
return deletions;
}
}