Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. def equalsWhenOneCharRemoved(x, y):
  2.  
  3. # Can't be different in length by more than 1
  4. if (abs(len(x) - len(y)) > 1):
  5. return False
  6.  
  7.  
  8. # Iterate through X and Y together
  9. diffFound = 0;
  10. lenX = len(x)
  11. lenY = len(y)
  12.  
  13. for i in range(max(lenX, lenY)):
  14. # If we have reached the end of either string, break
  15. if (i >= lenX - 1 or i >= lenY -1):
  16. break
  17.  
  18. # If they are different at an index, we add a difference
  19. if (x[i] != y[i]):
  20. diffFound += 1
  21.  
  22. # If there is more than one difference, they are not similar enough
  23. if (diffFound > 1):
  24. return False
  25.  
  26. # If we get here, check the number of differences is less than 1
  27. return diffFound <= 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement