Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Fuzzy String Matching by Sunshine_in_Winter
def fuzzy_string_match(str1: str, str2: str, threshold: int) -> bool:
# step 1 - define a variable to store the number of character difference
number_of_character_difference = 0
for i in range(min(len(str1), len(str2))):
if str1[i] != str2[i]:
number_of_character_difference += 1
number_of_character_difference += max(len(str1), len(str2)) - min (len(str1), len(str2))
return number_of_character_difference <= threshold
print("Example:")
print(fuzzy_string_match("apple", "appel", 2))
# These "asserts" are used for self-checking
assert fuzzy_string_match("apple", "appel", 2) == True
assert fuzzy_string_match("apple", "bpple", 1) == True
assert fuzzy_string_match("apple", "bpple", 0) == False
assert fuzzy_string_match("apple", "apples", 1) == True
assert fuzzy_string_match("apple", "bpples", 2) == True
assert fuzzy_string_match("apple", "apxle", 1) == True
assert fuzzy_string_match("apple", "pxxli", 3) == False
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 17, 2025
Comments: