-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsolution.cpp
61 lines (52 loc) · 2.19 KB
/
solution.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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
// Function to find the length of the longest common prefix between two arrays of integers
int longestCommonPrefix(vector<int> &arr1, vector<int> &arr2)
{
// Create an unordered map to store prefixes of numbers from arr1.
unordered_map<string, int> prefixMap;
// Step 1: Build the prefix map for all numbers in arr1
// Iterate over each number in arr1
for (int num : arr1)
{
// Convert the number into a string to handle its digits
string strNum = to_string(num);
string prefix = ""; // Initialize an empty string to build the prefix
// Iterate over each character (digit) of the stringified number
for (char ch : strNum)
{
// Append the current character (digit) to the prefix
prefix += ch;
// Insert the current prefix into the prefix map and increment its count
prefixMap[prefix]++;
}
}
// Variable to keep track of the maximum length of common prefix found
int maxLength = 0;
// Step 2: Check for common prefixes in arr2
// Iterate over each number in arr2
for (int num : arr2)
{
// Convert the number into a string to handle its digits
string strNum = to_string(num);
string prefix = ""; // Initialize an empty string to build the prefix
// Iterate over each character (digit) of the stringified number
for (char ch : strNum)
{
// Append the current character (digit) to the prefix
prefix += ch;
// Check if this prefix exists in the prefix map (i.e., it's a common prefix)
if (prefixMap.find(prefix) != prefixMap.end())
{
// Update maxLength to the maximum length of the common prefix found so far
maxLength = max(maxLength, static_cast<int>(prefix.length()));
}
}
}
// Return the length of the longest common prefix found
return maxLength;
}
};