Skip to content

Instantly share code, notes, and snippets.

@mjmeilahn
Last active May 3, 2023 23:34
Show Gist options
  • Save mjmeilahn/d9ee0daad38bc4bdf26341aaf1973765 to your computer and use it in GitHub Desktop.
Save mjmeilahn/d9ee0daad38bc4bdf26341aaf1973765 to your computer and use it in GitHub Desktop.
Python: Comparing Strings
# Compare TWO strings and return TRUE if the 2nd String only requires one change to match the original string.
# Otherwise return FALSE.
def compareStrings(orig, diff):
count = 0
for char in list(diff):
match = [i for i in list(orig) if i == char]
if len(match) == 1: count += 1
return True if count == (len(orig) - 1) else False
print(compareStrings('make', 'rake')) # True
print(compareStrings('make', 'mke')) # True
print(compareStrings('make', 'ake')) # True
print(compareStrings('make', 'ekam')) # False
print(compareStrings('make', 'make')) # False
print(compareStrings('make', 'rare')) # False
print(compareStrings('make', 'ke')) # False
print(compareStrings('make', 'ma')) # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment