You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md
+97-4
Original file line number
Diff line number
Diff line change
@@ -46,24 +46,117 @@ Note that common prefixes between elements of the same array do not count.
46
46
47
47
## Solutions
48
48
49
-
### Solution 1
49
+
### Solution 1: Hash Table
50
+
51
+
We can use a hash table to store all the prefixes of the numbers in `arr1`. Then, we traverse all the numbers $x$ in `arr2`. For each number $x$, we start from the highest bit and gradually decrease, checking whether it exists in the hash table. If it does, we have found a common prefix, and we can update the answer accordingly.
52
+
53
+
The time complexity is $O(m \times \log M + n \times \log N)$, and the space complexity is $O(m \times \log M)$. Here, $m$ and $n$ are the lengths of `arr1` and `arr2` respectively, and $M$ and $N$ are the maximum values in `arr1` and `arr2` respectively.
0 commit comments