#codeifyoucansolve
Given a string S which contains only lowercase characters [‘a’-‘z’] and an integer K you have to find number of substrings having weight equal to K.
Weight of characters is defined as :
Weight[‘a’]=1
Weight[‘b’]=2
Weight[‘c’]=3
Weight[‘d’]=4
Weight[‘e’]=5
Weight[‘f’]=6
Weight[‘g’]=7
Weight[‘h’]=8
Weight[‘i’]=9
Weight[‘j’]=10
Weight[‘k’]=11
Weight[‘l’]=12
Weight[‘m’]=13
Weight[‘n’]=14
Weight[‘o’]=15
Weight[‘p’]=16
Weight[‘q’]=17
Weight[‘r’]=18
Weight[‘s’]=19
Weight[‘t’]=20
Weight[‘u’]=21
Weight[‘v’]=22
Weight[‘w’]=23
Weight[‘x’]=24
Weight[‘y’]=25
Weight[‘z’]=26
Weight of a string will be defined as :
Weight[S[0]]+Weight[S[1]]+Weight[S[2]]……+Weight[S[Len-1]] where Len is length of string.
Similarly weight of substring string from index i and ending on position j will be defined as:
Weight[S[i]]+Weight[S[i+1]]+Weight[S[i+2]]……+Weight[S[j]]
Input:
First line of input contains number of test cases T and Each test case contains two lines first line contains value of integer K and second line contains the string S.
Output:
For each test case print the number of substrings having weight equal to K.
Constraints:
1<=T<=20
1<=K<=26*|S|
1<=|S|<=1000000
Sample Input (Plaintext Link)
2
5
abcdef
4
abcdef
Sample Output (Plaintext Link)
2
1
<?php
fscanf(STDIN,"%d\n",$T);
while($T--)
{
fscanf(STDIN,"%d\n",$K);
$str = trim(fgets(STDIN));
$str = str_split($str);
//print_r($str);
$count = 0;
foreach($str as $key => $value)
{
$num = ord($value) - 96;
for($i=$key;$i<count($str)-1;$i++)
{
if($num > $K)
break;
if($num === $K)
{
$count +=1;
break;
}
$num += (ord($str[$i+1]) - 96);
}
}
print($count."\n");
}
?>
Run : http://ideone.com/boEj3L