PracticeHashing

Match Shipment Assignment Patterns

Easy/MediumHashingtraversalstate trackingcomparison Time not estimated Not started

Context

A carrier records the sequence of shipment codes loaded onto a truck and the sequence of warehouse codes receiving those shipments. The actual names are not important; the repetition pattern is. Determine whether each distinct shipment code consistently corresponds to one distinct warehouse code throughout the two aligned records.

Problem

Given two equal-length arrays, shipment_codes and warehouse_codes, compare their positional patterns. A shipment code must always correspond to the same warehouse code wherever it appears. Also, two different shipment codes must never correspond to the same warehouse code. Return true only when every aligned position satisfies one consistent one-to-one correspondence. Return false if the arrays have different lengths or if any consistency or distinctness rule fails.

Examples

Example 1
Input: shipment_codes = ["alpha", "beta", "alpha", "gamma"]warehouse_codes = ["north", "south", "north", "east"]
Output: True
Explanation: The first example returns true because alpha consistently maps to north, beta to south, and gamma to east in the executed result.
Example 2
Input: shipment_codes = ["alpha", "beta", "alpha"]warehouse_codes = ["north", "south", "east"]
Output: False
Explanation: The second example returns false because alpha is retained as north while the later aligned position has alpha paired with east.
Example 3
Input: shipment_codes = ["alpha", "beta", "gamma"]warehouse_codes = ["north", "north", "east"]
Output: False
Explanation: The third example returns false because alpha is already paired with north when beta is also paired with north.

Constraints

  • 1 <= len(shipment_codes) <= 100
  • 1 <= len(shipment_codes[i]) <= 20 for every i
  • shipment_codes[i]: alphabet lowercase
  • len(shipment_codes) <= len(warehouse_codes) <= len(shipment_codes)
  • 1 <= len(warehouse_codes[i]) <= 20 for every i
  • warehouse_codes[i]: alphabet lowercase
  • shipment_codes contains only lowercase letters.
  • warehouse_codes contains only lowercase letters.
  • Types: shipment_codes is str[], warehouse_codes is str[]; result is bool

Function signature

def matches_assignment_pattern(shipment_codes: list[str], warehouse_codes: list[str]) -> bool
shipment_codes list[str]
The shipment code at each loading position.
warehouse_codes list[str]
The warehouse code assigned at each matching position.
returns bool
True when the two arrays have identical positional repetition structure under a one-to-one correspondence; otherwise false.

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

Notes

  • The correspondence must be one-to-one in both directions.
  • An expected linear-time scan with linear auxiliary bookkeeping is intended.
Approved · cb043-abs_528_v111 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1