Skip to content

Commit 33eab06

Browse files
solves alternating characters
1 parent 6763167 commit 33eab06

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://www.hackerrank.com/challenges/alternating-characters/problem
2+
3+
package strings;
4+
5+
import java.util.Scanner;
6+
7+
public class AlternatingCharacters {
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in);
10+
int queries = scanner.nextInt();
11+
while (queries-- > 0) {
12+
String string = scanner.next();
13+
System.out.println(minimumDeletions(string));
14+
}
15+
}
16+
17+
private static int minimumDeletions(String string) {
18+
char current = string.charAt(0);
19+
int deletions = 0;
20+
for (int index = 1 ; index < string.length() ; index++) {
21+
char character = string.charAt(index);
22+
if (current == character) {
23+
deletions++;
24+
} else {
25+
current = character;
26+
}
27+
}
28+
return deletions;
29+
}
30+
}

0 commit comments

Comments
 (0)