Trains the technique from
LeetCode 3076Shortest Uncommon Substring in an ArrayThis 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 warehouse labels every bin with a lowercase part code. The picking sheet wants, for each code, a short fragment that identifies it unambiguously.
You are given codes, an array of n part codes. A fragment of a code is any contiguous run of its characters. For each index i, report the shortest fragment of codes[i] that appears in no other code of the array -- appearing anywhere inside another code disqualifies it. When several fragments tie for shortest, report the alphabetically smallest of them. When no fragment of codes[i] qualifies, report the empty string for that index.
Return an array answer where answer[i] is the fragment reported for codes[i].
Example 1
For "cab" the fragments "a", "b" and "c" each fail to appear in "xyz", and "a" is the alphabetically smallest of them. For "xyz" the single characters all qualify, and "x" is alphabetically smallest.
Example 2
The fragment "a" appears in both codes, so it qualifies for neither. "b" appears only in the first code and "x" only in the second.
Example 3
The two codes are identical, so every fragment of one appears in the other and both indices report the empty string.
Example 4
For the first code, "z" appears in the second code but "zz" does not. The second code has only the fragment "z", which appears in the first code.
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 shortest_substrings(codes: list[str]) -> list[str]:public String[] shortestSubstrings(String[] codes)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.