Created
March 24, 2015 21:31
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
First it is broken, not very good solution but the point is to calculate the norm for string using the values of each character
Check this https://gist.github.com/aessam/aede5480b7bf8d36a714 :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you explain the idea behind it? 😃