Tuesday, April 5, 2016

Leonardo & the SubString - HackrRank Challenge


Leonardo & the SubString:

  • In this post we will solve the programming challenge of "Leonardo & the substring" matching posted in HackrRank.

Problem Description:

Leonardo loves puzzles involving strings, but he's just found a problem that has him stumped! Help him solve the following challenge:
Given a binary string, , composed of only 's and 's, find and print the total number of substrings of  which do not contain a  or .
Input Format
The first line contains an integer, (the number of test cases). 
The  subsequent lines of test cases each contain a string, , composed only of 's and 's.
Constraints
Output Format
For each test case, print the total number of substrings of  having no consecutive zeroes or ones (i.e.: not containing  or ).

Sample Input
4
1010
100
0000
11111
Sample Output
10
4
4
5
Explanation
Test Case 0
Our set of substrings There are possible substrings, none of which have consecutive 's or 's. Thus, we print on a new line.
Test Case 1 
Our set of substrings  
There are  possible substrings, but  of them (and ) have consecutive zeroes. Thus, we print the result of , which is , on a new line.

Algorithm:
  • The algorithm is explained with the below given example.
Input Binary String 0 0 1 0 1 1 0 0
Index 0 1 2 3 4 5 6 7


  1. Assume there are N elements (N=8 in our example) in the given binary string.
  2. Start from the index 0 and take a set of 2, 3, ... (N-stepnumber) of elements at a time, where stepnumber = 0, 1, 2.... (N-1). For the first iteration, the stepnumber is assigned to 0 and increments by 1 for each iteration.
  3. In each iteration, we will form a set with 2, 3 , ... etc., elements taken from the given input binary string. In each set, search for the sub-string 00 or 11. 
  4. If the sub-string match is found, in any one set, we can break that iteration and go for the next iteration. This is because, if the sub-string match is found in a set of taking x elements at a time, then for all the sets with elements higher than 'x' number of elements, we will be having the sub-string match;so we can break that iteration.
  5. Proceed the step number 3 & 4 till we reach the stepnumber (N-1).
Algorithm Illustration:

Stepnumber = 0:
  • Start from index = 0. Take 2 elements at a time;i.e {0,0}. This set is having the sub-string 00. So all other sets from index 0 [{001},{0010},{00101},{001011},{0010110},{00101100}] will be having the sub-string "00". So we can break that iteration when we find the first sub-string match.
Stepnumber = 1:
  • In this iteration, we will start from index 1. For this step, we can have a set with 2, 3, 4, .... (N-stepnumber) of elements. i.e. we can form a set with 2, 3, 4, .. 7 elements.
  • First a set with 2 elements {0,1} is taken. This set do not have any sub-string match. We can now take 3 elements at a time {010} and this too do not have any matching sub-string. The set of taking 4 elements({0101}) too do not have the matching sub-string. The first sub-string match occurs when we take the 5 elements at a time; i.e. {01011}. This set has the matching sub-string 11. So, the set of taking 6 elements {010110} & 7 elements {0101100} will be having the matching sub-string of 11. So, we can terminate this iteration after evaluating the set with 5 elements.

This process is continued till the setpnumber is 7.

C Coding:
  • Click on the below link for the "C" program to solve the above problem.


No comments:

Post a Comment