# Python Fundamentals Practice - Anagrams

I’m doing exercise about function and stuck at problem 13 - Anagrams. My code worked with provided input, but there is an error when submitting answer.
Can you guys help me take a look at it? Thanks a lot

My Code:

``````def are_anagrams(string1, string2):
if len(string1) != len(string2):
return False
arr = string1 + string2
freq = {}
for c in arr:
if c in freq:
freq[c] += 1
else:
freq[c] = 1
for c in freq:
if freq[c] == 1:
return False
return True

print(are_anagrams('gainly', 'laying'))
print(are_anagrams('banana', 'bacana'))
``````

What actually happened:

``````Function are_anagrams did not return the expected value.
``````

Please make sure to include the link to the Screen as well. It allows others to look at the context and try to run your code in order to better help you.

Hi, I’m stuck with problem 13 - Anagrams as well (Python Fundamentals Practice). My code worked (output matched expected output) but I got an error : Function `are_anagrams` did not return the expected value.

My Code:

`def are_anagrams (string1, string2):`

``````l = []
for x in string1:
for y in string2:
if x == y:
l.append(x)

if len (l) == len (string1) and len (l) == len (string2):
return True
else:
return False
``````

`print(are_anagrams(test1_string1,test1_string2))`
`print(are_anagrams(test2_string1,test2_string2))`

For each letter in string1 your code checks all letters in string2 and this would lead to the function returning True if string1 is “BBBBBB” and string2 is “BAAAAA”.

Hey, you may have already determined this given the date. However, your code only returns False if the two strings contain at least one different character.

Consider the strings ‘babb’ and ‘baab’. These are not anagrams, as the characters from one cannot be rearranged to become the other. However your code would return True for these strings. As freq would be {‘b’: 5, ‘a’: 3} (no key with the value equal to 1).