Weighted Uniform Strings

  • + 0 comments

    Perl:

    sub weightedUniformStrings {
        my ($s, $queries) = @_;
        my %weights;
        my $prev_char = '';
        my $current_weight = 0;
    
        foreach my $char (split //, $s) {
            my $char_weight = ord($char) - ord('a') + 1;
            
            if ($char eq $prev_char) {
                $current_weight += $char_weight;
            } else {
                $current_weight = $char_weight;
            }
    
            $weights{$current_weight} = 1;
            $prev_char = $char;
        }
    
        my @results;
        foreach my $query (@$queries) {
            push @results, exists $weights{$query} ? "Yes" : "No";
        }
    
        return @results;
    }