Sunday, February 10, 2019

Alternating Characters - Hacker Rank 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 are allowed to delete zero or more characters in the string.

Your task is to find the minimum number of required deletions.
For example, given the string , remove an  at positions  and  to make  in  deletions.
Function Description
Complete the alternatingCharacters function in the editor below. It must return an integer representing the minimum number of deletions to make the alternating string.
alternatingCharacters has the following parameter(s):
  • s: a string
Input Format
The first line contains an integer , the number of queries.
The next  lines each contain a string .
Constraints
  • Each string  will consist only of characters  and 
Output Format
For each query, print the minimum number of deletions required on a new line.
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4
Explanation
The characters marked red are the ones that can be deleted so that the string doesn't have matching consecutive characters.
image

Alternating Characters - Hacker Rank Solution

It is given in the question that the resultant string shouldn't have two adjacent matching characters.
If there are  adjacent matching characters, delete  of those characters and repeat this process to the end of the string.
See the implementation of the setter for more details.
Problem Setter's code:

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int t;
    cin >> t;
    while(t--) {
        string str;
        cin >> str;
        int ans = 0;
        for (int i = 0 ; i < str.length() - 1; i++) {
            if (str[i] == str[i + 1]) {
             // If two consecutive characters are the same, delete one of them.
                ans++;
            }
        }
        cout << ans << endl;
    }
    return 0;
}
Problem Tester's code:

Python 2

t = input()
for _ in range(t):
    s = raw_input()
    count = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            count += 1
    print count

7 comments:

  1. #!/bin/python3

    import math
    import os
    import random
    import re
    import sys

    # Complete the alternatingCharacters function below.
    def alternatingCharacters(s):
    count = 0
    for i in range(1,len(s)):
    if s[i] == s[i-1]:
    count += 1
    return(count)

    if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    q = int(input())

    for q_itr in range(q):
    s = input()

    result = alternatingCharacters(s)

    fptr.write(str(result) + '\n')

    fptr.close()

    ReplyDelete
  2. if i=3 the s[3] == s[3-1]
    # eg ababbba -> 1)i=3 ie i=b, i=[i-1] ie.i[3-1] =a b==a => 0 (condition false)
    2)if i=4 ie i=b, i=[i-1] ie.i[4-1] =b ->b==b => 1 (condition true)
    increase the counter

    ReplyDelete
  3. int count=0;

    for(int i=0 ; i<s.length()-1; i++)
    {
    if(s.charAt(i)==s.charAt(i+1))
    {
    count++;
    }

    }

    return count;

    ReplyDelete

Powered by Blogger.