• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anagrams pairs  RSS feed

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a problem where i have to find all the possible anagram pairs from the given file input. I have written a code for the same but it is working very slow.
Any idea what i am doing wrong:




Note: In another forum here, i have not pasted this part and dint ask same question. In this
i have asked question on "isAnagram()" method , i used in line 50.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say its working very slowly, do you mean it returns a result but takes along time or it keeps running and never returns a result?

If its the latter then I think I see a bug in the while loop in isAnagram(). If the string is matched with an anagram you remove it from the set, but if its not you leave it in the set. This means that if any word doesn't have a matching anagram you will pull that same word from the set on the next loop and check it again. This will go on forever.

If you meant the former then you will need to profile the code using a profiler and find the bottleneck.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike, Actually, it was case 2. Result was not coming even after long time.

I figured it out now, issue was with the while loop condition. I changed it and it worked fine now. Updated is as below:




But even after this, it slow. For example, for around 3000 i/p it took 9 sec but for others it was mentioned that for 10,000 input it took 2 sec. Any thought of improvement.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For performance improvements you may need to redesign your approach. Given that an anagram has the same letters but not in the same order, you can make a "key" for each word by making a string of the same letters where the letters are sorted in alphabetical order. Then you create a HashMap based on those keys. The HashMap would then contain a HashSet instead of a List so that no word is entered twice. In this way you can avoid the multiple comparisons of each word. Then, you'd have an anagram when the Set contained two or more words. This would also avoid the need for a sorted set.

Can you post your test set somewhere?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 47 the call to isAnagram() is being passed two Strings but the method only takes one String. Did you post your latest code?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My complete code is as below:



 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your approach looks better than me. Are you saying like below:

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Your approach looks better than me. Are you saying like below:


Yes, I think you've got the idea.
I'd still be interested in a test data set of 30,000.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will make changes as per new approach and let you know. My current approach will definitely takes several minutes.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!