• + 0 comments

    Perl:

    sub anagram {
        my $s = shift;
        my %h1;
        my $cnt = 0;
    
        if (length($s) % 2) {
            return -1;
        }
    
        my $s1 = substr($s, 0, length($s) / 2);
        my @s2 = split("", substr($s, length($s) / 2, length($s)));
    
        
        foreach (split("", $s1)) {
            $h1{$_} += 1;
        }
    
        foreach my $k (@s2) {
            if (defined($h1{$k}) && $h1{$k} != 0) {
                $h1{$k} -= 1;            
            }
            else {
                $cnt++;
            }        
        }
    
        return $cnt;
    }