• Post Reply Bookmark Topic Watch Topic
  • New Topic

Detailed Comparison of Two Strings with Nested Loops  RSS feed

 
Kris Zeli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this website has been really helpful in the past but I recently ran into something that nobody seems to have posted.

I'm trying to create a method that compares two strings in a way that determines if each letter from the first word is contained within the other exactly one time (like an anagram). Here is what I came up with:

The output is so far from what I need that I can't even understand what is going on. I feel like there should be an easier way to do this...
Thanks in advance.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris Zeli wrote:I feel like there should be an easier way to do this...

There is. What do you think would happen if you sorted both strings to put characters in alphabetical order?
You're also missing two very quick checks that will speed up the method a LOT.

Winston
 
Kris Zeli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha wow, thank you!!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris Zeli wrote:Haha wow, thank you!!

You're very welcome.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!