Algorithm
Words Within Two Edits of Dictionary
Solution
def dfs(self, word, i, node, cnt):
if cnt > 2 or not node: return False
if i == len(word): return node.isEnd
idx = ord(word[i]) - ord("a")
if self.dfs(word, i + 1, node.child[idx], cnt):
return True
for j in range(26):
if j != idx and self.dfs(word, i + 1, node.child[j], cnt + 1):
return True
return FalseVideo GuideLeetcode Daily
Time Complexity
O(q k n)
Space Complexity
O(1)
