• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anagrams pairs

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My complete code is as below:



 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your approach looks better than me. Are you saying like below:

 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic