Created
April 10, 2019 17:03
This file contains 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
def is_permutation(str1, str2): | |
len_str1 = len(str1) | |
len_str2 = len(str2) | |
if (len_str1 >= len_str2): | |
longer = str1 | |
shorter = str2 | |
else: | |
longer = str2 | |
shorter = str1 | |
map_longer = {char: 0 for char in longer} | |
map_shorter = {char: 0 for char in shorter} | |
# create a map of chars in longer with occurrence | |
for char in longer: | |
map_longer[char] += 1 | |
# create a map of chars in shorter with occurrence | |
for char in shorter: | |
map_shorter[char] += 1 | |
#check if all of chars in shorter exists in longer char | |
for char in shorter: | |
if map_shorter[char] > map_longer[char]: | |
return False | |
return True | |
str1 = 'mom' | |
str2 = 'grandmom' | |
print('%s, %s are permutation: %r'% (str1, str2, is_permutation(str1, str2))) | |
str3 = "momendum" | |
print('%s, %s are permutation: %r'% (str1, str3, is_permutation(str1, str3))) | |
str4 = "monday" | |
print('%s, %s are permutation: %r'% (str1, str4, is_permutation(str1, str4))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment