PracticeArrays

Spotting Synchronized Tokens

BeginnerArraystraversalaccumulationcomparison Time not estimated Not started

Context

A document review tool compares three versions of a note: an editor draft, a reviewer copy, and the published text. The versions may stop at different points. The tool needs to measure how many shared token positions contain exactly the same word in all three versions.

Problem

Write a function that counts aligned positions where the three supplied token lists contain identical words. Begin at position zero and inspect only positions that exist in every list. Add one for each position whose three words match exactly. Treat repeated words at different positions as separate matches. Stop when the shortest list ends, and return the count. Do not count distinct words globally, compare unaligned words, or inspect beyond the shared portion.

Examples

Example 1
Input: draft = ["the", "quick", "brown", "fox"]published = ["the", "quick", "brown", "dog"]review = ["the", "quick", "red", "fox"]
Output: 2
Explanation: The first example returns 2 because the executed result is 2, with final tally 2 after processing the shared range of length 4.
Example 2
Input: draft = ["go", "go", "stop"]published = ["go", "go", "stop"]review = ["go", "wait", "stop"]
Output: 2
Explanation: The second example returns 2 because the executed result is 2, with final tally 2 after processing the shared range of length 3.
Example 3
Input: draft = ["alpha", "beta"]published = ["alpha", "beta", "gamma"]review = ["alpha"]
Output: 1
Explanation: The third example returns 1 because the shared range has length 1 and the executed result is 1.

Constraints

  • 0 <= len(draft) <= 100
  • 1 <= len(draft[i]) <= 12 for every i
  • draft[i]: alphabet lowercase
  • 0 <= len(review) <= 100
  • 1 <= len(review[i]) <= 12 for every i
  • review[i]: alphabet lowercase
  • 0 <= len(published) <= 100
  • 1 <= len(published[i]) <= 12 for every i
  • published[i]: alphabet lowercase
  • draft, review, and published tokens contain only lowercase letters.
  • Types: draft is str[], review is str[], published is str[]; result is int

Function signature

def count_shared_token_positions(draft: list[str], review: list[str], published: list[str]) -> int
draft list[str]
The editor's token sequence.
review list[str]
The reviewer's token sequence.
published list[str]
The published token sequence.
returns int
The number of positions in the shared range where all three token lists contain the same word.

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

Notes

  • An empty token list is valid; then the shared range has no positions.
  • A word repeated at multiple positions is evaluated independently at each position.
Approved · c037-abs_596_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1