File tree Expand file tree Collapse file tree 1 file changed +42
-1
lines changed Expand file tree Collapse file tree 1 file changed +42
-1
lines changed Original file line number Diff line number Diff line change 1
- int alternatingCharacters (String s ) {
1
+ /* Program to find the total numbers of deletions needed to make a String with only alternating characters */
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #include <stdlib.h>
5
+
6
+ //Function which calculate and return total numbers of deletions
7
+ int alternatingCharacters (char * s ) {
8
+ //initialize required variables
2
9
int a = strlen (s );
3
10
int k = 0 ,i ;
11
+ //To verify all the letters of a String
4
12
for (i = 0 ;i < a ;i ++ )
5
13
{
14
+ //Comparing characters
15
+ //If both are same, then one has to deleted
6
16
if (s [i ]== s [i + 1 ])
17
+ //number of deletions to be done gets incremented
7
18
k ++ ;
8
19
}
9
20
return k ;
10
21
}
22
+ void main ()
23
+ {
24
+ //initialize required variables
25
+ //t = Number of test cases
26
+ int t ,ans ;
27
+ char str [20 ];
28
+ scanf ("%d" ,& t );
29
+ while (t -- ){
30
+ //For each test case get a String as input
31
+ scanf ("%s" ,str );
32
+ //Print the number of deletions required for each String
33
+ printf ("%d\n" ,alternatingCharacters (str ));
34
+ }
35
+ }
36
+ /*
37
+ Test case 1:
38
+ Input: 5
39
+ AAAA
40
+ BBBBB
41
+ ABABABAB
42
+ BABABA
43
+ AAABBB
44
+ Output: 3
45
+ 4
46
+ 0
47
+ 0
48
+ 4
49
+ Time Complexity O(n) where n is the length of string
50
+ */
51
+
You can’t perform that action at this time.
0 commit comments