Word Pattern
Real-world version: Does The Shift Code Fit The Bin RunThe 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.
def code_fits_run(code: str, run: str) -> bool:public boolean codeFitsRun(String code, String run)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's