Alternating Characters – HackerRank Problem Solution


You are given a string containing characters and only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you can delete zero or more characters in the string.

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

For example, given the string s=AABAAB, remove an A at positions 0 and 3  to make  s=ABAB in  deletions.

Input format:

The first line contains an integer q, the number of queries.

The next q lines each contain a string s.

Output format:

For each query, print the minimum number of deletions required on a new line.

Write a program to find count to make string alternative





Here is original problem from HackerRank


Solution Logic:

  • Iterate on each char of string
  • Check if current char is equals (=) to next char then increment the count by one.

Leave a Reply

Your email address will not be published. Required fields are marked *