Skip to content

Instantly share code, notes, and snippets.

@aessam
Created March 24, 2015 21:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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.")
@hendawy
Copy link

hendawy commented Mar 25, 2015

Can you explain the idea behind it? 😃

@aessam
Copy link
Author

aessam commented Mar 26, 2015

First it is broken, not very good solution but the point is to calculate the norm for string using the values of each character

@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