sub is_anagram {
  my $word1 = lc shift;
  my $word2 = lc shift;

  my %seen=();
  my $sum=0;
  my $n=length($word1);
  for(my $i=0; $i<$n; $i++) {
    my $char = substr($word1, $i, 1);
    $sum += my $value = $seen{$char} = index($word2, $char, $seen{$char}||0)+1;
  }

  return ($sum == ($n*$n+$n)/2);
}

while (<DATA>) {
  my ($w1, $w2) = split;
  print "$w1 and $w2 are anagrams\n" if is_anagram($w1, $w2);
}

__DATA__
abc xxxxxa
eat robust
carrot nosehair
