-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathfind-the-length-of-the-longest-common-prefix.py
51 lines (48 loc) · 1.22 KB
/
find-the-length-of-the-longest-common-prefix.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
# Time: O((n + m) * l)
# Space: O(t)
# trie
class Solution(object):
def longestCommonPrefix(self, arr1, arr2):
"""
:type arr1: List[int]
:type arr2: List[int]
:rtype: int
"""
_trie = lambda: collections.defaultdict(_trie)
trie = _trie()
for x in arr1:
reduce(dict.__getitem__, str(x), trie)
result = 0
for x in arr2:
curr = trie
for i, c in enumerate(str(x)):
if c not in curr:
break
curr = curr[c]
else:
i += 1
result = max(result, i)
return result
# Time: O((n + m) * l)
# Space: O(n)
# hash table
class Solution2(object):
def longestCommonPrefix(self, arr1, arr2):
"""
:type arr1: List[int]
:type arr2: List[int]
:rtype: int
"""
lookup = {0}
for x in arr1:
while x not in lookup:
lookup.add(x)
x //= 10
result = 0
for x in arr2:
l = len(str(x))
while x not in lookup:
x //= 10
l -= 1
result = max(result, l)
return result