Show problems as
Classic problem #290EasyHash TableString

Word Pattern

Real-world version: Does The Shift Code Fit The Bin Run
Tracked in this browser only
Read Word Pattern on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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