PracticeSorting

Match the Warehouse Label Contents

EasySortingsortingcomparisontraversal Time not estimated Not started

Context

A warehouse receives two shipment labels for parcels that may have been printed in different character orders. The labels are made only of lowercase letters. The dispatch team needs to know whether both labels contain exactly the same letters, including repeated letters, even if their order differs.

Problem

Write a function that checks whether two shipment labels contain the same multiset of lowercase letters. The order of letters does not matter, but every repeated letter must appear the same number of times in both labels. Return true when the labels have equal lengths and matching letter counts; otherwise return false. An empty label is valid and matches another empty label. Do not modify the input strings.

Examples

Example 1
Input: label_a = "staple"label_b = "plates"
Output: True
Explanation: The executed call for "staple" and "plates" returned true. Both lengths were 6, and both ordered sequences were ["a", "e", "l", "p", "s", "t"].
Example 2
Input: label_a = "letter"label_b = "teller"
Output: False
Explanation: The executed call for "letter" and "teller" returned false. The ordered sequences were ["e", "e", "l", "r", "t", "t"] and ["e", "e", "l", "l", "r", "t"], and the final position was 3 with result false.
Example 3
Input: label_a = ""label_b = ""
Output: True
Explanation: The executed call for two empty labels returned true. Both ordered sequences were empty, the starting position was 0, and the final result was true.

Constraints

  • 0 <= len(label_a) <= 100
  • label_a: alphabet lowercase
  • 0 <= len(label_b) <= 100
  • label_b: alphabet lowercase
  • label_a and label_b contain only lowercase letters.
  • Types: label_a is str, label_b is str; result is bool

Function signature

def labels_have_same_contents(label_a: str, label_b: str) -> bool
label_a str
The first lowercase shipment label.
label_b str
The second lowercase shipment label.
returns bool
Return true exactly when both labels contain identical letters with identical multiplicities, regardless of order.

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

Notes

  • Whitespace and punctuation do not occur because labels contain only lowercase letters.
  • Empty labels are permitted.
Approved · cb041-abs_396_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1