Skip to content

Instantly share code, notes, and snippets.

@arun057
Created November 13, 2019 01:58
Show Gist options
  • Save arun057/51aa10df9c375c78bbd723e81a0f193d to your computer and use it in GitHub Desktop.
Save arun057/51aa10df9c375c78bbd723e81a0f193d to your computer and use it in GitHub Desktop.
def equalsWhenOneCharRemoved(x, y):
lenX = len(x)
lenY = len(y)
# If the lengths are not different by 1, its automatically not true
if abs(lenX - lenY) != 1:
return False
count = 0
i = 0
j = 0
while i < lenX and j < lenY:
# If the characters dont match, check the count.
if x[i] != y[j]:
if count == 1:
return False
if lenX > lenY:
i += 1
elif lenX < lenY:
j += 1
else:
i += 1
j += 1
count += 1
else:
i += 1
j += 1
# if the extra character is at the end.
if i < lenX and j < lenY:
count += 1
return count == 1
# assert equalsWhenOneCharRemoved("x", "y") is False
# assert equalsWhenOneCharRemoved("x", "XX") is False
# assert equalsWhenOneCharRemoved("yy", "yx") is False
# assert equalsWhenOneCharRemoved("abcd", "abxcd") is True
# assert equalsWhenOneCharRemoved("xyz", "xz") is True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment