###Bug###
Bug
Your algorithm is not quite correct. For example:
areAnagrams("abc", "abc") -> true (correct)
areAnagrams("abc", "abcd") -> true (wrong)
areAnagrams("abcd", "abc") -> false (correct)
The problem is that you are checking that the characters in s1
exist with the same frequency in s2
. But there can be characters in s2
that do not exist in s1
and you don't check for those.
An easy way to remedy this would be to check that both strings have the same length. This plus the frequency check you are already doing should ensure that the strings are anagrams.
###Bug###
Your algorithm is not quite correct. For example:
areAnagrams("abc", "abc") -> true (correct)
areAnagrams("abc", "abcd") -> true (wrong)
areAnagrams("abcd", "abc") -> false (correct)
The problem is that you are checking that the characters in s1
exist with the same frequency in s2
. But there can be characters in s2
that do not exist in s1
and you don't check for those.
An easy way to remedy this would be to check that both strings have the same length. This plus the frequency check you are already doing should ensure that the strings are anagrams.
Bug
Your algorithm is not quite correct. For example:
areAnagrams("abc", "abc") -> true (correct)
areAnagrams("abc", "abcd") -> true (wrong)
areAnagrams("abcd", "abc") -> false (correct)
The problem is that you are checking that the characters in s1
exist with the same frequency in s2
. But there can be characters in s2
that do not exist in s1
and you don't check for those.
An easy way to remedy this would be to check that both strings have the same length. This plus the frequency check you are already doing should ensure that the strings are anagrams.
###Bug###
Your algorithm is not quite correct. For example:
areAnagrams("abc", "abc") -> true (correct)
areAnagrams("abc", "abcd") -> true (wrong)
areAnagrams("abcd", "abc") -> false (correct)
The problem is that you are checking that the characters in s1
exist with the same frequency in s2
. But there can be characters in s2
that do not exist in s1
and you don't check for those.
An easy way to remedy this would be to check that both strings have the same length. This plus the frequency check you are already doing should ensure that the strings are anagrams.