14 September 2014

Quiz 30: Find number of keys which need to get repaired

Problem:
Given number of broken keyboard keys and a string. Find how many keys need to be repair to type this string

Input Format: 
Broken keys without any spaces
String to be typed

Output Format: 
Number of keys which need to be repaired

Constraints: 
none

Sample Input
aspzq
happiness

Sample Output:
3

Explanations:
keys a,s and p need to be repaired


Solution:

chomp($n=<STDIN>);
@br=split("",$n);
$len=@br;
$count=0;
chomp($str=<STDIN>);
@arr=split("",$str);
$len1=@arr;
for($j=0;$j<$len;$j++)
{
if($str =~ /$br[$j]/)
{
$count++;
}
}
print "$count";


Tips:
To type p two times, key which need to repaired is key p, so count will not be doubled.

No comments:

Post a Comment