The task is:
A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper-case English letters A, C, G, T.
<
perl 100/100 solution:
sub solution {
my ($S, $P, $Q)=@_; my @P=@$P; my @Q=@$Q;
my @_A = (0), @_C = (0), @_G = (0), @ret =();
foreach (split //, $S)
{
push @_A, $_A[-1] + ($_ eq 'A' ? 1 : 0);
push @_C, $_C[-1] + ($_ eq 'C' ? 1 : 0);
push @_G, $_G[-1] + ($_ eq 'G' ? 1 : 0);
}
foreach my $i (0..$#P)
{
my $from_index = $P[$i];
my $to_index = $Q[$i] + 1;
if ( $_A[$to_index] - $_A[$from_index] > 0 )
{
push @ret, 1;
next;
}
if ( $_C[$to_index] - $_C[$from_index] > 0 )
{
push @ret, 2;
next;
}
if ( $_G[$to_index] - $_G[$from_index] > 0 )
{
push @ret, 3;
next;
}
push @ret, 4
}
return @ret;
}