PracticeHashing

Shared Token-Code Projection

Easy/MediumHashingtraversalfilteringset membership Time not estimated Not started

Context

A document archive stores each document as an ordered list of numeric token codes. The archive wants to compare two documents while ignoring codes that occur in only one document. Repeated codes remain repeated, and their original order must be preserved.

Problem

Given two ordered integer arrays, keep every occurrence whose value appears at least once in both arrays. Perform this filtering independently on each array, without sorting or removing duplicates. Return true exactly when the two resulting arrays have the same length and equal values at every corresponding position. If either input is empty, apply the same rule: its filtered result is empty.

Examples

Example 1
Input: codes_a = [7, 2, 7, 4]codes_b = [9, 7, 2, 7]
Output: True
Explanation: Example 1 executes with both projections equal to [7, 2, 7], so the result is true (fact_ids:, ).
Example 2
Input: codes_a = [3, 1, 3, 2]codes_b = [1, 3, 2, 3]
Output: False
Explanation: Example 2 executes with projections [3, 1, 3, 2] and [1, 3, 2, 3], so the result is false (fact_ids:, ).
Example 3
Input: codes_a = [1, 2, 3]codes_b = [4, 5]
Output: True
Explanation: Example 3 executes with both projections empty, so the result is true (fact_ids:, ).

Constraints

  • 0 <= len(codes_a) <= 100000
  • -1000000000 <= codes_a[i] <= 1000000000 for every i
  • 0 <= len(codes_b) <= 100000
  • -1000000000 <= codes_b[i] <= 1000000000 for every i
  • Types: codes_a is int[], codes_b is int[]; result is bool

Function signature

def have_matching_shared_projection(codes_a: list[int], codes_b: list[int]) -> bool
codes_a list[int]
The first document's ordered token-code sequence.
codes_b list[int]
The second document's ordered token-code sequence.
returns bool
True when the two filtered sequences of shared values are identical in order and multiplicity; otherwise false.

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

Notes

  • An occurrence is retained whenever its numeric value exists anywhere in the other input.
  • Repeated shared values are retained with their original multiplicity and order.
Approved · c029-abs_962_v314 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1