Created
November 19, 2014 07:13
-
-
Save binhngoc17/1dc7dadf23452f0bb456 to your computer and use it in GitHub Desktop.
anagram checker
This file contains hidden or 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
# 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