All problems
0463EasyHash TableString

Does The Shift Code Fit The Bin Run

Tracked in this browser only
Write code

Trains the technique from

LeetCode 290Word Pattern

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A sorting line is set up from two written records of the same shift. code is a string of lowercase letters, one letter per item that came down the line. run names the bin each item was dropped into, the bin names given as lowercase words separated by single spaces, with no space at either end.

The two records fit when the letters and the bin names stand in for each other exactly: every occurrence of a given letter goes with the same bin name, and every occurrence of a given bin name goes with the same letter. So one letter may not cover two different bins, and one bin may not answer to two different letters. The two records must also describe the same number of items.

Return true if the two records fit and false otherwise. A bin name is one whole word: two names that merely share a first letter are different names.

Examples

Example 1

Input
code = "mnm", run = "rust amber rust"
Output
true

The letter m goes with rust both times it appears and n goes with amber, and neither bin is shared with another letter.

Example 2

Input
code = "pq", run = "amber amber"
Output
false

The two letters are different but both items went into amber, so one bin would have to answer to two letters.

Example 3

Input
code = "wzwz", run = "chalk slate slate chalk"
Output
false

The letter w goes with chalk and z with slate at the first two items, and the last two items reverse that pairing.

Constraints

  • 1 <= code.length <= 300
  • code contains only lowercase English letters.
  • 1 <= run.length <= 3000
  • run contains only lowercase English letters and the space character ' '.
  • run has no leading or trailing space.
  • The bin names in run are separated by exactly one space.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def code_fits_run(code: str, run: str) -> bool:
Java
public boolean codeFitsRun(String code, String run)
September 7
Apply