You are viewing a single comment's thread. Return to all comments →
I wrote a horribly inefficient solution in perl, and it worked! That made me question my existance!!!!
sub sherlockAndAnagrams($) { my ($st)=@_; my @a=split(//, $st); my %h; for(my $i=0; $i<=$#a; ++$i) { for(my $j=0; $j<=$#a-$i; ++$j) { my $s=join "", sort(@a[$j..$j+$i]); if(exists($h{$s})) { $h{$s}+=1; } else { $h{$s}=1; } } } my $totals=0; for my $k (keys(%h)) { $totals+= $h{$k}*($h{$k}-1)/2; } return $totals; }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
I wrote a horribly inefficient solution in perl, and it worked! That made me question my existance!!!!