All problems
0707MediumArrayStringBit Manipulation

Pairing Ribbons on the Label Printer

Tracked in this browser only
Write code

Trains the technique from

LeetCode 318Maximum Product of Word Lengths

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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

Python
def best_ribbon_pairing(codes: list[str]) -> int:
Java
public int bestRibbonPairing(String[] codes)
September 7
Apply