Show problems as
Classic problem #318MediumArrayStringBit Manipulation

Maximum Product of Word Lengths

Real-world version: Pairing Ribbons on the Label Printer
Tracked in this browser only
Read Maximum Product of Word Lengths on LeetCode(opens in a new tab)

The 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.

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