Skip to content

Instantly share code, notes, and snippets.

@aessam
Created March 24, 2015 21:31
Show Gist options
  • Save aessam/e515a92542fc41ea5911 to your computer and use it in GitHub Desktop.
Save aessam/e515a92542fc41ea5911 to your computer and use it in GitHub Desktop.
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):
norm = 0
for c in s:
if not c==" ":
norm+=math.pow(ord(c),2)
return math.sqrt(norm)
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.")
@aessam
Copy link
Author

aessam commented Mar 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment