Suppose we have a list of lowercase strings called words where each word is of same length. We have to check whether there are two strings that differ only in one character.
So, if the input is like words = ["seed", "pick", "lick", "root", "live"], then the output will be True, as "pick" and "lick" are almost same.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(words): s = set() for word in words: for i, w in enumerate(word): if word[:i] + "*" + word[i + 1 :] in s: return True else: s.add(word[:i] + "*" + word[i + 1 :]) return False words = ["seed", "pick", "lick", "root", "live"] print(solve(words))
["seed", "pick", "lick", "root", "live"]
True