Skip to content

Commit 2a0cb15

Browse files
Update alternating_characters.c
1 parent 22ca727 commit 2a0cb15

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

Strings/alternating_characters.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
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
29
int a=strlen(s);
310
int k=0,i;
11+
//To verify all the letters of a String
412
for(i=0;i<a;i++)
513
{
14+
//Comparing characters
15+
//If both are same, then one has to deleted
616
if(s[i]==s[i+1])
17+
//number of deletions to be done gets incremented
718
k++;
819
}
920
return k;
1021
}
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+

0 commit comments

Comments
 (0)