Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
using collections.defaultdict solution in Clear category for Surjection Strings by pacurar.sebastian90
from collections import defaultdict
def isometric_strings(str1: str, str2: str) -> bool:
"""
2 variables: "entries" which is a defaultdict defaulting to set() as values of keys,
"isometric" which is a boolean to return if the strings are isometric or not.
first step is to add the keys and correlated value(s) to "entries". Since "entries"
is a dictionary which defaults to set() as values, using k.add(v) is easier to handle
to check if isometric is True, is to verify if the length of each entries key is 1,
and if any key has a value of higher than 1 as set length, then "isometric" is set to False
"""
entries, isometric = defaultdict(set), True
for i in range(len(str2)):
str1_letter, str2_letter = str1[i], str2[i]
entries[str1_letter].add(str2_letter)
for k in entries.keys():
if len(entries[k]) > 1:
isometric = False
return isometric
if __name__ == '__main__':
print("Example:")
print(isometric_strings('add', 'egg'))
# These "asserts" are used for self-checking and not for an auto-testing
assert isometric_strings('add', 'egg') == True
assert isometric_strings('foo', 'bar') == False
assert isometric_strings('', '') == True
assert isometric_strings('all', 'all') == True
print("Coding complete? Click 'Check' to earn cool rewards!")
March 7, 2021