cswork / OAOpen practice workspace

UberOA #36

Find Length of Longest Common Prefix

EasyJudge-ready

Topics (AI-labelled)Trie & CodecHashing

You are given two arrays arr1 and arr2 of positive integers. Write each integer as its usual decimal string; pick one number from arr1 and one from arr2 and compare their common prefix starting from the most significant digit. Return the length of the longest common prefix over all pairs; if no pair shares a first digit, the result is 0. The prefix may equal one entire number; for example, the common prefix of 123 and 12345 has length 3.

Valid input range on this site: 1≤|arr1|,|arr2|≤50000; 1≤arr[i]≤10^9. The ranges above are added by this site, and the array lengths are on the same scale as the constraints of LeetCode 3043.

Input

The first line contains n m; the second line contains n positive integers for arr1; the third line contains m positive integers for arr2.

Output

Output the length of the longest common prefix of the decimal representations over all pairs taken from the two arrays.

Example 1

Input:

4 4
123 4 5 955
12345 63 95 2

Output:

3

Example 2

Input:

1 1
7
8

Output:

0

Example 3

Input:

1 1
9
9

Output:

1

Example 4

Input:

1 1
10
1000

Output:

2

Example 5

Input:

2 2
1200 7
1234 12

Output:

2

Example 6

Input:

2 2
1 98
10 99

Output:

1

Example 7

Input:

1 1
999999999
999999999

Output:

9

Example 8

Input:

2 2
2 123456789
2 123456780

Output:

8

Example notes

Store every non-empty prefix of each integer in arr1 in a set, then scan the prefixes of each number in arr2 and update the maximum length. You can stop scanning a number at its first prefix that is not in the set.

Editorial

Loading solution…