forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
words-within-two-edits-of-dictionary.py
66 lines (56 loc) · 1.83 KB
/
words-within-two-edits-of-dictionary.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Time: O(25 * l * (n + q))
# Space: O(25 * l * n)
import string
# hash
class Solution(object):
def twoEditWords(self, queries, dictionary):
"""
:type queries: List[str]
:type dictionary: List[str]
:rtype: List[str]
"""
MOD = (1<<64)-59 # largest 64-bit prime
BASE = 113
POW = [1]
def add(a, b):
return (a+b)%MOD
def mult(a, b):
return (a*b)%MOD
def pow(i):
while not (i < len(POW)):
POW.append(mult(POW[-1], BASE))
return POW[i]
lookup = set()
for w in dictionary:
h = reduce(lambda h, i: add(h, mult(ord(w[i])-ord('a'), pow(i))), xrange(len(w)), 0)
for i, c in enumerate(w):
for x in string.ascii_lowercase:
if x == c:
continue
lookup.add(add(h, mult(ord(x)-ord(c), pow(i))))
result = []
for w in queries:
h = reduce(lambda h, i: add(h, mult(ord(w[i])-ord('a'), pow(i))), xrange(len(w)), 0)
for i, c in enumerate(w):
for x in string.ascii_lowercase:
if x == c:
continue
if add(h, mult(ord(x)-ord(c), pow(i))) in lookup:
break
else:
continue
result.append(w)
break
return result
# Time: O(q * n * l)
# Space: O(1)
import itertools
# brute force
class Solution2(object):
def twoEditWords(self, queries, dictionary):
"""
:type queries: List[str]
:type dictionary: List[str]
:rtype: List[str]
"""
return [q for q in queries if any(sum(c1 != c2 for c1, c2 in itertools.izip(q, d)) <= 2 for d in dictionary)]