|
| 1 | +# 3043. Find the Length of the Longest Common Prefix |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Hash Table, String, Trie. |
| 5 | +- Similar Questions: Longest Common Prefix, Longest Common Suffix Queries. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given two arrays with **positive** integers `arr1` and `arr2`. |
| 10 | + |
| 11 | +A **prefix** of a positive integer is an integer formed by one or more of its digits, starting from its **leftmost** digit. For example, `123` is a prefix of the integer `12345`, while `234` is **not**. |
| 12 | + |
| 13 | +A **common prefix** of two integers `a` and `b` is an integer `c`, such that `c` is a prefix of both `a` and `b`. For example, `5655359` and `56554` have a common prefix `565` while `1223` and `43456` **do not** have a common prefix. |
| 14 | + |
| 15 | +You need to find the length of the **longest common prefix** between all pairs of integers `(x, y)` such that `x` belongs to `arr1` and `y` belongs to `arr2`. |
| 16 | + |
| 17 | +Return **the length of the **longest** common prefix among all pairs**.** If no common prefix exists among them**, **return** `0`. |
| 18 | + |
| 19 | + |
| 20 | +Example 1: |
| 21 | + |
| 22 | +``` |
| 23 | +Input: arr1 = [1,10,100], arr2 = [1000] |
| 24 | +Output: 3 |
| 25 | +Explanation: There are 3 pairs (arr1[i], arr2[j]): |
| 26 | +- The longest common prefix of (1, 1000) is 1. |
| 27 | +- The longest common prefix of (10, 1000) is 10. |
| 28 | +- The longest common prefix of (100, 1000) is 100. |
| 29 | +The longest common prefix is 100 with a length of 3. |
| 30 | +``` |
| 31 | + |
| 32 | +Example 2: |
| 33 | + |
| 34 | +``` |
| 35 | +Input: arr1 = [1,2,3], arr2 = [4,4,4] |
| 36 | +Output: 0 |
| 37 | +Explanation: There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. |
| 38 | +Note that common prefixes between elements of the same array do not count. |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +**Constraints:** |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +- `1 <= arr1.length, arr2.length <= 5 * 104` |
| 47 | + |
| 48 | +- `1 <= arr1[i], arr2[i] <= 108` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +```javascript |
| 55 | +/** |
| 56 | + * @param {number[]} arr1 |
| 57 | + * @param {number[]} arr2 |
| 58 | + * @return {number} |
| 59 | + */ |
| 60 | +var longestCommonPrefix = function(arr1, arr2) { |
| 61 | + var trie = {}; |
| 62 | + for (var i = 0; i < arr1.length; i++) { |
| 63 | + var str = `${arr1[i]}`; |
| 64 | + var map = trie; |
| 65 | + for (var j = 0; j < str.length; j++) { |
| 66 | + if (!map[str[j]]) { |
| 67 | + map[str[j]] = {}; |
| 68 | + } |
| 69 | + map = map[str[j]]; |
| 70 | + } |
| 71 | + } |
| 72 | + var max = 0; |
| 73 | + for (var i = 0; i < arr2.length; i++) { |
| 74 | + var str = `${arr2[i]}`; |
| 75 | + var map = trie; |
| 76 | + var len = 0; |
| 77 | + for (var j = 0; j < str.length; j++) { |
| 78 | + if (!map[str[j]]) { |
| 79 | + break; |
| 80 | + } |
| 81 | + len += 1; |
| 82 | + map = map[str[j]]; |
| 83 | + } |
| 84 | + max = Math.max(max, len); |
| 85 | + } |
| 86 | + return max; |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +**Explain:** |
| 91 | + |
| 92 | +Trie. |
| 93 | + |
| 94 | +**Complexity:** |
| 95 | + |
| 96 | +* Time complexity : O(n * m). |
| 97 | +* Space complexity : O(n * m). |
0 commit comments