-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2452-words-within-two-edits-of-dictionary.py
More file actions
34 lines (28 loc) · 948 Bytes
/
2452-words-within-two-edits-of-dictionary.py
File metadata and controls
34 lines (28 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# time complexity: O(n^2)
# space complexity: O(n)
from typing import List
class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
def validDiff(query, diction):
count = 0
for i in range(len(query)):
if query[i] != diction[i]:
count += 1
return count <= 2
result = []
for query in queries:
flag = False
for diction in dictionary:
if flag:
continue
if validDiff(query, diction):
result.append(query)
flag = True
continue
return result
queries = ["word", "note", "ants", "wood"]
dictionary = ["wood", "joke", "moat"]
print(Solution().twoEditWords(queries, dictionary))
queries = ["yes"]
dictionary = ["not"]
print(Solution().twoEditWords(queries, dictionary))