Trains the technique from
LeetCode 472Concatenated WordsThis 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 parts catalogue lists labels in labels. Every label is a non-empty string of lowercase letters and no label appears twice.
A label is compound when it can be cut into two or more pieces, laid end to end in order, where every piece is itself a label in the catalogue. The pieces may repeat, so a label may be built by using one catalogue label several times, and the piece order is simply left to right through the compound label. Because a compound label needs at least two pieces and no piece may be empty, every piece is strictly shorter than the compound label it builds.
A label that can only be produced as one whole piece, namely itself, is not compound.
Return every compound label in the catalogue, in any order.
Example 1
`"abcd"` cuts into `"ab"` and `"cd"`, two catalogue labels laid end to end. `"abc"` has cuts `"a"` plus `"bc"` and `"ab"` plus `"c"`, and neither pair is in the catalogue. `"ab"` and `"cd"` are two letters each and the catalogue has no single-letter label.
Example 2
`"ab"` is `"a"` then `"b"`, `"ba"` is `"b"` then `"a"`, `"aba"` is `"ab"` then `"a"`, and `"bab"` is `"ba"` then `"b"`. The single letters `"a"` and `"b"` cannot be cut into two non-empty pieces at all.
Example 3
The catalogue has one label, and it can only be produced as itself in one piece, which the rules do not count.
The values you return may be in any order.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def find_compound_labels(labels: list[str]) -> list[str]:public List<String> findCompoundLabels(String[] labels)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.