Since the input is guaranteed to be strings of spaces and uppercase letters, removing the spaces and comparing the sorted strings will do in Python 3:
def isanagram_slow(s1, s2): if [a for a in sorted(s1) if a!=" "]==[b for b in sorted(s2) if b!=" "]: return 1 return 0 # return 1 if sorted(s1)==sorted(s2) else 0
This is inefficient but compact easy version. Should whitespace be treated like any other char, the hashed line is the whole function in an even more compact form.
Intermediate difficulty version:
def isanagram(s1, s2): A=[0 for i in range(27)] B=[0 for i in range(27)] # A=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] # B=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] for i in s1: if i == " ": pass # if i == " ": A+=1 else: A[ord(i)-64]+=1 for i in s2: if i == " ": pass # if i == " ": B+=1 else: B[ord(i)-64]+=1 return 1 if A == B else 0
A & B will hold the amount of char occurencies in each string. In this case, position 0 counts spaces, others count letters. According to wikipedia, whitespaces don't count (eg "A B C" == "ABC") so I pass them, but they can be enabled by uncommenting the hashed tests.
Should list comprehension be considered to put a dent in the O(m+n) time for shorter strings, A&B arrays can be defined like the hashed examples. The code could easily be scaled to handle all the chars recognized by Python at the memory cost of 2*1,114,111 bytes (valid chr() range according to docs) for the A & B arrays.