while loop to check anagrams in python

1.5k views Asked by At

I have the following exercise: Two words are anagrams if you can rearrange the letters from one to spell the other. Write a function called is_anagram that takes two strings and returns True if they are anagrams.

I have developed a function, but it is not working properly and I cannot find out why. Could anybody tell me what I am doing wrong? Thank you very much.

def isa(s,t):  
    if len(s)!=len(t):  
        print "impossible"  
    if len(s)==len(t):  
        i=0  
        while i<len(s)-1:  
            for i in s:  
                if i in t:  
                    print "yay"  
                print "NO"
1

There are 1 answers

2
inspectorG4dget On
import collections

def is_anagram(s,t):
    return collections.Counter(s) == collections.Counter(t)