diff --git a/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/README.md b/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/README.md index a0c087176..7fe2b48af 100755 --- a/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/README.md +++ b/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/README.md @@ -1,28 +1,36 @@ # [3043.Find the Length of the Longest Common Prefix][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two arrays with **positive** integers `arr1` and `arr2`. + +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**. + +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. + +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`. + +Return the length of the **longest** common prefix among all pairs. If no common prefix exists among them, return `0`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: arr1 = [1,10,100], arr2 = [1000] +Output: 3 +Explanation: There are 3 pairs (arr1[i], arr2[j]): +- The longest common prefix of (1, 1000) is 1. +- The longest common prefix of (10, 1000) is 10. +- The longest common prefix of (100, 1000) is 100. +The longest common prefix is 100 with a length of 3. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find the Length of the Longest Common Prefix -```go ``` - +Input: arr1 = [1,2,3], arr2 = [4,4,4] +Output: 0 +Explanation: There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. +Note that common prefixes between elements of the same array do not count. +``` ## 结语 diff --git a/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution.go b/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution.go index d115ccf5e..cc0627f9b 100644 --- a/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution.go +++ b/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution.go @@ -1,5 +1,47 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "strconv" +) + +type trieNode3043 struct { + child [10]*trieNode3043 +} + +func (r *trieNode3043) insert(intStr string) { + walker := r + for i := range intStr { + index := intStr[i] - '0' + if walker.child[index] == nil { + walker.child[index] = &trieNode3043{child: [10]*trieNode3043{}} + } + walker = walker.child[index] + } +} + +func (r *trieNode3043) prefixMatch(intStr string) int { + walker := r + i := 0 + for ; i < len(intStr); i++ { + index := intStr[i] - '0' + if walker.child[index] == nil { + break + } + walker = walker.child[index] + } + return i +} + +func Solution(arr1 []int, arr2 []int) int { + ans := 0 + tree := &trieNode3043{} + for _, intVal := range arr1 { + v := strconv.Itoa(intVal) + tree.insert(v) + } + for _, intVal := range arr2 { + v := strconv.Itoa(intVal) + ans = max(ans, tree.prefixMatch(v)) + } + return ans } diff --git a/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution_test.go b/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution_test.go index 14ff50eb4..77a4e97e8 100644 --- a/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution_test.go +++ b/leetcode/3001-3100/3043.Find-the-Length-of-the-Longest-Common-Prefix/Solution_test.go @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + a1, a2 []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 10, 100}, []int{1000}, 3}, + {"TestCase2", []int{1, 2, 3}, []int{4, 4, 4}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.a1, c.a2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.a1, c.a2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }