-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_permutation.py
33 lines (25 loc) · 899 Bytes
/
is_permutation.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
#use hash tables
def permutation(astring,bstring):
hash1 ={}
hash2 ={}
if len(astring) != len(bstring):
return False
for char in astring:
if char in hash1:
hash1[char] += 1
hash1[char] = 1
for char in bstring:
if char in hash2:
hash2[char] += 1
hash2[char] = 1
return hash1 == hash2
"""
Describe what it means for two strings to be permutations of each other. Now, look at
that definition you provided. Can you check the strings against that definition?
Two strings that are permutations should have the same characters, but in different
orders. Can you make the orders the same?
Could a hash table be useful?
There is one solution that is 0 (N log N) time. Another solution uses some space, but
isO(N) time.
"""
# instead of using hash dict use counter from collections