Alternating Characters


/*
 Shashank likes strings in which consecutive characters are different. For example, he likes ABABA, while he doesn't
 like ABAA. Given a string containing characters A and B only, he wants to change it into a string he likes. To do this,
 he is allowed to delete the characters in the string.
Your task is to find the minimum number of required deletions.
Input Format
The first line contains an integer T, i.e. the number of test cases.
The next T lines contain a string each.
Output Format
For each test case, print the minimum number of deletions required.
Constraints
1<=T<=10

1<= length of string <=10^5
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
 */

using System;

namespace HackerDemo
{
    class AlternatingCharacters
    {
        public void AlternatingCharactersMain()
        {
            int t = Convert.ToInt32(Console.ReadLine());
            for (int a0 = 0; a0 < t; a0++)
            {
                string str = Console.ReadLine();
                Console.WriteLine(GetCount(str));
            }
        }

        public int GetCount(string str)
        {
            int count = 0;
            for (int i = 0; i < str.Length-1; i++)
            {
                if (str[i]==str[i+1])
                {
                    count++;
                }
            }
            return count;
        }
    }
}

No comments:

Post a Comment