Alternating character

Your task is to find the minimum number of required deletions.


Input Format
The first line contains an integer , i.e. the number of test cases.
The next lines contain a string each.
Output Format
For each test case, print the minimum number of deletions required.
Constraints

length of string
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4
Explanation
AAAA A, 3 deletions
BBBBB B, 4 deletions
ABABABAB ABABABAB, 0 deletions
BABABA BABABA, 0 deletions
AAABBB AB, 4 deletions because to convert it to AB we need to delete 2 A's and 2 B's

solution:

#include <cstdio>
#include<string.h>
#include <iostream>

int main() {
    int i,c=0,t,n;
char arr[100];

cin>>t;
while(t!=0)
{   c=0;
    cin>>arr;    

n=strlen(arr);
for(i=0;i<n-1;i=i+1)
{
if(arr[i]=='A'&&arr[i+1]=='B'||arr[i]=='B'&&arr[i+1]=='A')
;
    else
        c++;
}
   t--;
cout<<c<<endl;
}
    
    return 0;
}

Comments

Popular posts from this blog

Pre-compiled Headers

CONNECTING WITH FTP-FILE TRANSFER PROTOCOL

PROBING THE INTERNET WITH TELNET: