diff --git a/problems/3001-3300/p3043/solution_3043.go b/problems/3001-3300/p3043/solution_3043.go new file mode 100644 index 0000000..d37ee99 --- /dev/null +++ b/problems/3001-3300/p3043/solution_3043.go @@ -0,0 +1,31 @@ +package p3043 + +import "strconv" + +func longestCommonPrefix(arr1 []int, arr2 []int) int { + prefixes := make(map[int]struct{}) + + for _, num := range arr1 { + for num > 0 { + prefixes[num] = struct{}{} + num /= 10 + } + } + + ans := 0 + + for _, num := range arr2 { + for num > 0 { + if _, ok := prefixes[num]; ok { + numStr := strconv.Itoa(num) + if len(numStr) > ans { + ans = len(numStr) + break + } + } + num /= 10 + } + } + + return ans +} diff --git a/problems/3001-3300/p3043/solution_3043_test.go b/problems/3001-3300/p3043/solution_3043_test.go new file mode 100644 index 0000000..7fef547 --- /dev/null +++ b/problems/3001-3300/p3043/solution_3043_test.go @@ -0,0 +1,13 @@ +package p3043 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSolution3043(t *testing.T) { + assert.Equal(t, 3, longestCommonPrefix([]int{1, 10, 100}, []int{1000}), "example 1") + assert.Equal(t, 0, longestCommonPrefix([]int{1, 2, 3}, []int{4, 4, 4}), "example 2") + assert.Equal(t, 4, longestCommonPrefix([]int{5655359, 1223}, []int{56554, 789}), "shared multi-digit prefix") +}