Maximum Product of Word Lengths
Real-world version: Pairing Ribbons on the Label PrinterThe statement here is our own: the same inputs, outputs and technique.
A label printer is loaded with a set of ribbons. Ribbon i carries the code codes[i], written in lowercase English letters.
The printer has one ink channel per letter of the alphabet, so two ribbons can run together only when no letter appears on both of them. The span of such a pairing is the product of the lengths of the two codes.
Return the largest span over all pairings the printer can run. If no two ribbons can run together, return 0.
Examples
Example 1
- Input
- codes = ["mint", "sable", "orchid", "gum"]
- Output
- 30
The ribbons carrying `sable` and `orchid` share no letter, so they can run together, and their lengths multiply to `5 * 6 = 30`.
Example 2
- Input
- codes = ["ember", "brace", "camber"]
- Output
- 0
`ember` and `brace` both use `b`, `ember` and `camber` both use `m`, and `brace` and `camber` both use `c`. No pairing can run, so the answer is 0.
Example 3
- Input
- codes = ["ox", "zip"]
- Output
- 6
The only pairing uses `ox` and `zip`, which share no letter, so the span is `2 * 3 = 6`.
Constraints
- 2 <= codes.length <= 1000
- 1 <= codes[i].length <= 1000
- codes[i] consists only of lowercase English letters.
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 best_ribbon_pairing(codes: list[str]) -> int:public int bestRibbonPairing(String[] codes)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 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
- 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
- Maximum Length of a Concatenated String with Unique CharactersReal-world version: Longest Stencil Run Without A Repeated LetterMedium
- Maximum Score Words Formed by LettersReal-world version: Best Tile Rack PlayHard