File tree 1 file changed +30
-0
lines changed
1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments