Skip to content

Instantly share code, notes, and snippets.

@hoyajigi
Created January 11, 2020 07:41
Show Gist options
  • Save hoyajigi/3cdbf57581fa220362d1ec8d3a094543 to your computer and use it in GitHub Desktop.
Save hoyajigi/3cdbf57581fa220362d1ec8d3a094543 to your computer and use it in GitHub Desktop.
def equalsWhenOneCharRemoved(x, y):
if abs(len(x) - len(y)) != 1:
return False
if len(x) > len(y):
x, y = y, x
i = padding = 0
while i < len(x) and i + padding < len(y):
if x[i] == y[i + padding]:
i = i + 1
else:
padding += 1
if i == len(x) and padding < 2:
return True
return False
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
assert equalsWhenOneCharRemoved("xyz", "xy") is True
assert equalsWhenOneCharRemoved("yxz", "xz") is True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment