-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFIndTheLengthOfLongestCommonPrefix.cpp
More file actions
49 lines (38 loc) · 1.2 KB
/
FIndTheLengthOfLongestCommonPrefix.cpp
File metadata and controls
49 lines (38 loc) · 1.2 KB
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
// 3043. Find the Length of the Longest Common Prefix: https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix/description/
// medium, hashset, trie
/*
iterate over the first array
and store the 10 reverse-factored each values in the hashset
iterate over the second array
check if the int contains in the hashset, eliminate single digit each time
from the array
check for until the longest length is found
return the longest length
*/
class Solution {
public:
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
unordered_set <int> allPrefixes;
for(int val : arr1)
{
while(!allPrefixes.count(val) && val > 0)
{
allPrefixes.insert(val);
val /= 10;
}
}
int longestPrefixLength = 0;
for(int val : arr2)
{
while(!allPrefixes.count(val) && val > 0)
{
val /= 10;
}
if(val > 0)
{
longestPrefixLength = max(longestPrefixLength, static_cast<int>(log10(val) + 1));
}
}
return longestPrefixLength;
}
};