Skip to content

Commit 01659af

Browse files
authored
Create 3043. Find the Length of the Longest Common Prefix (#594)
2 parents 927c48a + 2ebd13c commit 01659af

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
4+
set<int> st;
5+
for (auto num : arr1) {
6+
while (num > 0) {
7+
st.insert(num);
8+
num /= 10;
9+
}
10+
}
11+
int maxi = 0;
12+
for (auto num : arr2) {
13+
int temp = num;
14+
while (temp > 0) {
15+
16+
if (st.find(temp) != st.end()) {
17+
int len = (int)log10(temp) + 1;
18+
maxi = max(maxi, len);
19+
break;
20+
}
21+
temp /= 10;
22+
}
23+
}
24+
return maxi;
25+
}
26+
};

0 commit comments

Comments
 (0)