Word Break II
Real-world version: Tape Track SplitsThe statement here is our own: the same inputs, outputs and technique.
An archive holds reel tapes whose printed labels ran the track names together with every separator worn away. You are given the label tape and titles, the catalogue of track names the archive recognises.
Restore the label. A restoration is an ordered run of catalogue titles whose letters, written one after another with nothing between them, give tape exactly. A title may be reused as often as it helps, and the catalogue may hold titles that no restoration uses.
Return every restoration as a string, with one blank space between neighbouring titles. The restorations may come back in any order. Return an empty list when the label cannot be restored from the catalogue at all.
Examples
Example 1
- Input
- tape = "dubdub", titles = ["dub", "du", "b", "bdub"]
- Output
- ["du b du b", "du b dub", "du bdub", "dub du b", "dub dub"]
Three runs of titles spell the label: dub then dub, du then bdub, and du then b then du then b.
Example 2
- Input
- tape = "harpsong", titles = ["harp", "song", "harps", "on", "g"]
- Output
- ["harp song", "harps on g"]
The label splits after harp and after harps, so two restorations exist and both are reported.
Example 3
- Input
- tape = "mix", titles = ["mi"]
- Output
- []
No catalogue title covers the trailing x, so nothing can be restored and the list is empty.
Constraints
- 1 <= tape.length <= 20
- 1 <= titles.length <= 1000
- 1 <= titles[i].length <= 10
- tape and titles[i] hold lowercase English letters only
- The titles are pairwise distinct
- The input is chosen so the returned list holds at most 10^5 restorations
The values you return may be in any order.
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 tape_track_splits(tape: str, titles: list[str]) -> list[str]:public List<String> tapeTrackSplits(String tape, List<String> titles)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
More on the same topics
- Word BreakReal-world version: Label Splits Into CodesMedium
- Stickers to Spell WordReal-world version: Cutting Letters Out of StencilsHard
- Number of Matching SubsequencesReal-world version: Labels That Read Through the TapeMedium
- Maximum Score Words Formed by LettersReal-world version: Best Tile Rack PlayHard