-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram.py
52 lines (40 loc) · 1.05 KB
/
anagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 18 17:44:17 2019
@author: Himanshu
"""
def anagram(s,t):
# remove whitespaces
s = s.replace(" ","").lower()
t = t.replace(" ","").lower()
if not s or not t:
return False
# if len(s)!= len(t):
# return False
total = {}
for char in s:
if char in total:
total[char] += 1
else:
total[char] = 1
for char in t:
if char in total:
total[char] -= 1
else:
return False
for c in total:
if total[c]!=0:
return False
return True
from nose.tools import assert_equal
class AnagramTest(object):
def test(self,sol):
assert_equal(sol('go go go',''),False)
assert_equal(sol('abc','cba'),True)
assert_equal(sol('hi man','hi man'),True)
assert_equal(sol('aabbcc','aabbc'),False)
assert_equal(sol('123','1 2'),False)
print('ALL TEST CASES PASSED')
# Run Tests
t = AnagramTest()
t.test(anagram)