The Love-Letter Mystery

  • + 0 comments

    Perl solution:

    sub theLoveLetterMystery {
        my @s = split("", shift);
        my $cnt = 0;
        my $k = 0;
        my $left_part = 0;
        my $right_part = 0;
        while ($k < int(scalar(@s) / 2)) {
            $left_part = ord($s[$k]);
            $right_part = ord($s[scalar(@s) - $k - 1]);
            while ($left_part != $right_part){
                if ($left_part < $right_part) {
                    $cnt++;
                    $right_part -= 1;
                } elsif ($left_part > $right_part) {
                    $cnt++;
                    $left_part -= 1;
                }
            }
            $k++;
        }
        return $cnt;
    
    }