PracticeArrays

Locate the Extra Document Token

EasyArraystraversalcomparisonindex arithmetic Time not estimated Not started

Context

A proofreading tool compares a clean token list with a revised list. The revised document contains every token from the clean list in the same order, plus one additional token. The tool needs the index where the aligned sequences first separate, using zero-based positions.

Problem

Given a reference token sequence and a revised token sequence, return the earliest zero-based index at which their aligned tokens differ. The revised sequence contains exactly one additional token while preserving the order of every reference token. Compare positions from the beginning and stop at the first mismatch. If all positions of the reference sequence match the corresponding revised positions, return the reference sequence length. This boundary result also applies when the reference sequence is empty or the additional token is at the end. Repeated tokens are allowed; report the first mismatch produced by the forward alignment rule, not an inferred insertion among indistinguishable repeated tokens.

Examples

Example 1
Input: reference_tokens = ["the", "quick", "brown", "fox"]revised_tokens = ["the", "quick", "small", "brown", "fox"]
Output: 2
Explanation: Example 1 returns 2 because the first differing aligned position is index 2.
Example 2
Input: reference_tokens = ["read", "write", "share"]revised_tokens = ["read", "write", "share", "learn"]
Output: 3
Explanation: Example 2 returns 3 because all three reference positions agree, so the boundary result is the reference length.
Example 3
Input: reference_tokens = ["go", "go", "home"]revised_tokens = ["go", "stop", "go", "home"]
Output: 1
Explanation: Example 3 returns 1 because the forward alignment finds the first mismatch at index 1, even with repeated tokens.

Constraints

  • 0 <= len(reference_tokens) <= 100
  • 1 <= len(reference_tokens[i]) <= 15 for every i
  • reference_tokens[i]: alphabet lowercase
  • len(reference_tokens) + 1 <= len(revised_tokens) <= len(reference_tokens) + 1
  • 1 <= len(revised_tokens[i]) <= 15 for every i
  • revised_tokens[i]: alphabet lowercase
  • reference_tokens and revised_tokens contain only lowercase letters.
  • Types: reference_tokens is str[], revised_tokens is str[]; result is int

Function signature

def find_extra_token_position(reference_tokens: list[str], revised_tokens: list[str]) -> int
reference_tokens list[str]
The original document tokens in order.
revised_tokens list[str]
The tokens after one additional token was inserted.
returns int
The zero-based position of the first aligned mismatch, or the length of reference_tokens when every reference position matches.

Adapted from MBPP problem task_890 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · c019-abs_290_v111 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1