FunctionRacerProblemsRace

Common Start

BeginnerTwo pointersPython, JavaScript, TypeScript, Java, C++

Return how many items the lists a and b share at the start: the largest n for which the first n items of a are the same as the first n items of b, in the same order.

The two lists can have different lengths, and either of them may be empty.

Example 1

a [1,2,3,4]
b [1,2,5,4]
returns 2

Both lists start with 1, 2. Then 3 meets 5, so the common start has 2 items.

Example 2

a [7,8]
b [7,8,9]
returns 2

All of a matches the start of b, and then a runs out.

Example 3

a [3]
b [4]
returns 0

The very first items differ.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems