Skip to content

Instantly share code, notes, and snippets.

@aessam
Created March 24, 2015 21:31
Show Gist options
  • Select an option

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

Select an option

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

hendawy commented Mar 25, 2015

Copy link
Copy Markdown

Can you explain the idea behind it? 😃

@aessam

aessam commented Mar 26, 2015

Copy link
Copy Markdown
Author

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

aessam commented Mar 26, 2015

Copy link
Copy Markdown
Author

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