I have made a function that determines whether 2 words are an anagram or not. My Problem is: that if the word contains 2 or more of the same letters(eg hello, arrest, gall) the function doesn't work even when the 2 words input are an anagram,
If i input "ate" & "eat" my function returns true - so it works
But if I input "mello" & "ollem" - I know these are not words but it should still return true, but it doesn't because the repeated 'l' confuses the function...I think?
I didn't want to poke but firstPerson's method doesn't take into account the frequency of characters, so for inputs
like s1 = roorsseett
s2 = sosretetet
returns true.
letter s1 s2
r 2 1
o 2 1
.
.
or inputs like
s1 = rorre
s2 = oroee
returns true.
Another method would be to sort the strings(may be using quicksort) and then check character by character, if both the strings are anagrams, then after sorting both s1[index] must be equal to s2[index] else strings are not anagrams.
Another thing to take into account are whether there are spaces, and to strip them before sorting.While capitalized characters should be converted into lower case before sorting.
You'll also have to decide whether to take into characters other than alphanumeric ones. Consider
s1 = O, Draconian Devil
s2 = Leonardo Da Vinci
you have got to remove that comma in any method you follow as well as the spaces
Last edited by zalezog; Aug 15th, 2009 at 8:49 am.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.