Maximum Length of a Concatenated String with Unique Characters
Real-world version: Longest Stencil Run Without A Repeated LetterThe statement here is our own: the same inputs, outputs and technique.
A print shop owns a rack of stencil strips, strips[i] being the lowercase text engraved on strip i. To make a banner the shop picks some of the strips, keeping them in rack order, and butts the chosen texts together into one run.
A run is only printable when no letter shows up twice anywhere in it. That rules out reusing a letter across two chosen strips, and it also rules out any strip whose own engraving repeats a letter: such a strip cannot appear in a printable run at all, not even on its own.
The shop may pick as many or as few strips as it likes, including none, and a run made of no strips has length 0.
Return the greatest length a printable run can have.
Examples
Example 1
- Input
- strips = ["fox", "glib", "fog"]
- Output
- 7
Picking strips 0 and 1 gives the run `"foxglib"`, which is seven characters long and uses each of `f`, `o`, `x`, `g`, `l`, `i`, `b` once.
Example 2
- Input
- strips = ["mummy", "pen", "quid"]
- Output
- 7
Strip 0 engraves `m` three times, so it cannot appear in any printable run. Picking strips 1 and 2 gives `"penquid"`, seven characters with no letter twice.
Example 3
- Input
- strips = ["aa", "bb"]
- Output
- 0
Both strips repeat a letter within their own engraving, so neither may be picked and the only printable run is the empty one.
Constraints
- 1 <= strips.length <= 16
- 1 <= strips[i].length <= 26
- strips[i] consists of lowercase English letters only.
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 max_unique_run(strips: list[str]) -> int:public int maxUniqueRun(String[] strips)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 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
- Maximum Score Words Formed by LettersReal-world version: Best Tile Rack PlayHard
- Stickers to Spell WordReal-world version: Cutting Letters Out of StencilsHard
- Count the Number of Consistent StringsReal-world version: Labels the Press Can SetEasy
- Count Pairs Of Similar StringsReal-world version: Pairs of Labels Using the Same LettersEasy