Skip to content

Instantly share code, notes, and snippets.

@aessam
Created March 26, 2015 22:55
Show Gist options
  • Select an option

  • Save aessam/aede5480b7bf8d36a714 to your computer and use it in GitHub Desktop.

Select an option

Save aessam/aede5480b7bf8d36a714 to your computer and use it in GitHub Desktop.
[Better and some how shouldn't be broken] Solving Anagram check problem using python and Linear Algebra and the complexity is O(n), which is 2nd lovely after constant :D
import math
def stringNorm(s):
odd = 0
even = 0
for c in s:
if not c==" ":
if ord(c)%2==0:
even+=math.pow(ord(c),2)
else:
odd+=math.pow(ord(c),2)
return math.pow(odd,2)+math.pow(even,2)
def anagram_detection(s1,s2):
return stringNorm(s1)==stringNorm(s2)
s1 = input("Please enter first string: ").lower()
s2 = input("Please enter second string: ").lower()
print ("Anagram.") if anagram_detection(s1,s2) else print ("Not Anagram.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment