Repeated Document Snippets
Context
A proofreading tool scans short token sequences extracted from documents. Identical sequences should share one record, even when they appear in different documents. You will count how often each distinct ordered sequence occurs, preserving the order in which distinct sequences first appear.
Problem
Given a collection of documents represented as lists of tokens, count the occurrences of every distinct document sequence. Two sequences are identical only when they have the same tokens in the same order. Return one frequency for each distinct sequence, ordered by that sequence's first appearance while scanning the outer collection from left to right and each document from left to right. An empty collection returns an empty list. Repeated sequences contribute to the same frequency, and an empty inner sequence is also a valid sequence.
Examples
documents = [["alpha", "beta"], ["gamma"], ["alpha", "beta"], ["gamma"]][2, 2]documents = [["red"], ["red", "blue"], [], ["red"], ["red", "blue"], []][2, 2, 2]documents = [["one", "two", "one"], ["two", "one"], ["one", "two", "one"], ["three"]][2, 1, 1]Constraints
0 <= len(documents) <= 1000 <= len(documents[i]) <= 20 for every i1 <= len(documents[i][j]) <= 20 for every i, jdocuments[i][j]: alphabet lowercasedocuments tokens contain only lowercase letters.- Types:
documentsis str[][]; result is int[]
Function signature
def count_sequence_occurrences(documents: list[list[str]]) -> list[int]
documentslist[list[str]]- A collection of token sequences extracted from documents.
- returns list[int]
- The frequencies of distinct sequences, listed in first-appearance order.
Adapted from MBPP problem task_779 (CC BY 4.0). Rewritten, extended and verified by Iksha.
Notes
- The result order is determined by first appearance, not by alphabetical sorting.
- An empty inner sequence is valid and must be counted like any other exact sequence.