LC 1554 [M] Strings Differ by One Character - ALawliet/algorithms GitHub Wiki
T: O(nm^2)
class Solution:
def differByOne(self, dict: List[str]) -> bool:
# only need one pass to solve the problem using hashset:
seens = set()
for word in dict:
for i, c in enumerate(word):
# change 'abcd' into the form of '*bcd', 'a*cd' ...
masked_word = word[:i] + '*' + word[i+1:]
if masked_word in seens:
return True
else:
seens.add(masked_word)
return False