12 September 2014

Quiz 24: Find number of players removed

Problem:
There are 5 houses in school and they are represented as R,G,B,Y,L. A school teacher ask his students to stand in a row. Now, if 2 or more students of same house stand next to each other, the school teacher keeps only 1 of them in row and removes the rest. Find number if students removed.

Input Format: 
N-number of test cases
Followed by N lines denoting the row formation

Output Format: 
Number of students removed for each case in different lines

Constraints: 
none

Sample Input
3
RGBYL
RRRRGGGBBYYLLRRRGG
RGGRGRGRGRGYYLYBBRGBYL

Sample Output:
0
11
3

Explanations:-
Case 3, students marked in RED are removed, 3 of them- RGGRGRGRGRGYYLYBBRGBYL

Solution:

@output=();
chomp($T=<STDIN>);
for($i=0;$i<$T;$i++)
{
chomp($N=<STDIN>);
@arr=split("",$N);
$len=@arr;
        $count=0;
for($j=0;$j<$len;$j++)
{
if($arr[$j] eq $arr[$j+1])
{
$count++;
}
}
push(@output,$count);
}
foreach(@output)
{
print "$_\n";
}

Tips:
Simple enough, just compare two consecutive items and increase count if matches

No comments:

Post a Comment