Yes exactly. And now I hope to convince you that it's good to learn a
few languages so you can use the best one for the problem at hand.  In
Perl for example, your proposed algorithm looks like this:

while (<>) {
  chomp;
  push @{ $map{join('', sort split('', $_))} }, $_;
}
while ( (undef, $val) = each %map ) {
  print join(' ', @$val)."\n" if @$val > 1;
}

On my litltle laptop this writes to a file all the anagrams in a
dictionary of about 250,000 words in 4 seconds.

My favorite is:

anatomicopathologic pathologicoanatomic

And there are three 9-fold anagrams:

angor argon goran grano groan nagor orang organ rogan
ester estre reest reset steer stere stree terse tsere
caret carte cater crate creat creta react recta trace

Cheers.
On May 12, 4:43 am, Jeevitesh <jeeviteshshekha...@gmail.com> wrote:
> Yup i agree with Atul's solution.
>
> Just explaining the same thing a little better.
> Have a HashMap with the following structure:-
>
> HashMap<String, ArrayList<String>>;
>
> now scan through the List of words Sort the word and check whether it is
> already there on the hashmap if it is just add this word to the list for
> this corresponding word else just add this sorted form of this word to the
> HashMap .
>
> So if you give me any word i will sort it check it in hashmap and get the
> list of all its corresponding anagrams.
>
>
>
>
>
> On Sat, May 12, 2012 at 1:57 PM, atul anand <atul.87fri...@gmail.com> wrote:
> > given the list of words... what you can do is the following :-
> > now take first word from the list..
> > sort this word in alphabetical order...for eg str=bcda ---> sort , we get
> > str=abcd
> > now considering this sorted word as a key(abcd) , insert original word
> > (bcda as value) into the hash table ( hash table with chaining )
>
> > similarly do it for other words.
>
> > now given a word , you just need to sort this given word and use it as a
> > key to fetch all anagram.
>
> > On Fri, May 11, 2012 at 5:24 PM, mayur <mayursa...@gmail.com> wrote:
>
> >> Hi all,
>
> >> I am stuck with a question for a long time...can someone provide the best
> >> algorithm for this..
>
> >> Question).. find all the anagrams in a list of words. The algorithm
> >> should be efficient as the list can be very large.
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "Algorithm Geeks" group.
> >> To view this discussion on the web visit
> >>https://groups.google.com/d/msg/algogeeks/-/c4cSIMcBYLEJ.
> >> To post to this group, send email to algogeeks@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> algogeeks+unsubscr...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/algogeeks?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to algogeeks@googlegroups.com.
> > To unsubscribe from this group, send email to
> > algogeeks+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
>
> --
> *Thanks,
> Jeevitesh Shekhar Singh.*

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to