-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_permutation.py
55 lines (41 loc) · 1.5 KB
/
check_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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
# god, dog => permutation
# god, odg => permutation
# the, cat # not permutation
# good dog # not a permutation
def check_permutation(string1, string2):
# check for length:
# if not equal, return false
# if equal:
# turn string1 into a list
# loop through each character in string2:
# if character in the list, pop the character from the list
# if the string1 list is empty:
# return true
if len(string1) != len(string2):
return False
string1_dict = {}
for letter in string1:
if letter not in string1_dict:
string1_dict[letter] = 1
else:
string1_dict[letter] += 1
# string1_lst = list(string1)
# for letter in string2:
# if letter in string1_lst:
# string1_lst.pop(string1_lst.index(letter))
# if len(string1_lst) == 0:
# return True
for letter in string2:
print(letter)
if letter not in string1_dict:
return False
if letter in string1_dict:
string1_dict[letter] -= 1
if set(string1_dict.values()) == {0}:
return True
return False
print(check_permutation("god", "dogo")) # false
print(check_permutation("god", "dog")) # true
print(check_permutation("god", "cat")) # false
print(check_permutation("", "")) # true