-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-the-length-of-the-longest-common-prefix.cpp
82 lines (75 loc) · 1.91 KB
/
find-the-length-of-the-longest-common-prefix.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Time: O((n + m) * l)
// Space: O(t)
// trie
class Solution {
private:
class Trie {
public:
Trie()
: nodes_() {
new_node();
}
void add(const string& w) {
int curr = 0;
for (int i = 0; i < size(w); ++i) {
const int k = w[i] - '0';
if (nodes_[curr][k] == -1) {
nodes_[curr][k] = new_node();
}
curr = nodes_[curr][k];
}
}
int query(const string& w) {
int result = 0, curr = 0;
for (int i = 0; i < size(w); ++i) {
const int k = w[i] - '0';
if (nodes_[curr][k] == -1) {
return i;
}
curr = nodes_[curr][k];
}
return size(w);
}
private:
int new_node() {
nodes_.emplace_back(vector<int>(10, -1));
return size(nodes_) - 1;
}
vector<vector<int>> nodes_;
};
public:
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
Trie trie;
for (const auto& x : arr1) {
trie.add(to_string(x));
}
int result = 0;
for (const auto& x : arr2) {
result = max(result, trie.query(to_string(x)));
}
return result;
}
};
// Time: O((n + m) * l)
// Space: O(n)
// hash table
class Solution2 {
public:
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
unordered_set<int> lookup = {0};
for (auto x : arr1) {
for (; x; x /= 10) {
lookup.emplace(x);
}
}
int result = 0;
for (auto x : arr2) {
int l = size(to_string(x));
for (; !lookup.count(x); x /= 10) {
--l;
}
result = max(result, l);
}
return result;
}
};