Skip to content

Instantly share code, notes, and snippets.

@binhngoc17
Created November 19, 2014 07:13
Show Gist options
  • Save binhngoc17/1dc7dadf23452f0bb456 to your computer and use it in GitHub Desktop.
Save binhngoc17/1dc7dadf23452f0bb456 to your computer and use it in GitHub Desktop.
anagram checker
# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
import fileinput
input = fileinput.input()
s1 = input[0].replace('\n', '')
s2 = input[1].replace('\n', '')
dict_s1 = {}
dict_s2 = {}
# Gather character dictionary of the two strings
for c in s1:
dict_s1[c] = dict_s1.get(c, 0) + 1
for c in s2:
dict_s2[c] = dict_s2.get(c, 0) + 1
print dict_s1
print dict_s2
# Compare two character dictionaries of the two strings
keys = set(dict_s1.keys() + dict_s2.keys())
anagram = True
for k in keys:
if dict_s1.get(k, 0) != dict_s2.get(k, 0):
anagram = False
if anagram:
print 'Anagrams!'
else:
print 'Not anagrams!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment