Highlight the Shortest Unique Substring in Each Label
AtlassianSoftware Engineer · Technical ScreenLast updated
Find and highlight the shortest case-insensitive unique substring in each label using bracket notation, breaking ties by earliest index and preserving original casing.
Problem Statement
In a dropdown menu component, keyboard navigation shortcuts are displayed by highlighting a trigger character sequence inside each item's label. To avoid user confusion, each shortcut sequence must be unique across all available labels.
Given a list of strings labels, transform each label by enclosing its shortest unique substring inside square brackets [ and ].
A substring of labels[i] is considered unique if its lowercase version does not appear as a substring in the lowercase version of any other labels[j] (where j != i).
Follow these rules for each label:
- Choose the shortest unique substring.
- If there is a tie in length, choose the one with the earliest starting index.
- The uniqueness check is case-insensitive, but the final output must preserve the original casing of
labels[i]. - If no unique substring exists for a label, leave the label unchanged.
Function Signature
def highlight_shortcuts(labels: list[str]) -> list[str]:
pass
Constraints
1 <= len(labels) <= 1001 <= len(label) <= 50for eachlabelinlabels- Labels consist of English letters, digits, and spaces.
Examples
Example 1
Input: labels = ["apple", "banana", "cat"]
Output: ["a[p]ple", "[b]anana", "[c]at"]
Explanation:
- For
"apple":'a'appears in"banana"and"cat". The next character'p'(index 1) does not appear in any other label. It has length 1, which is optimal, resulting in"a[p]ple". - For
"banana":'b'(index 0) is not in"apple"or"cat", resulting in"[b]anana". - For
"cat":'c'(index 0) is not in"apple"or"banana", resulting in"[c]at".
Example 2
Input: labels = ["pull", "push", "pullet"]
Output: ["pull", "pu[s]h", "pull[e]t"]
Explanation:
- For
"pull": Every substring of"pull"(including the full word itself) appears inside"pullet". Hence, no unique substring exists, and it remains unchanged. - For
"push":'p'and'u'appear in"pull"and"pullet".'s'(index 2) is unique and has length 1, resulting in"pu[s]h". - For
"pullet": Substrings of length 1:'p','u','l'appear in"pull".'e'(index 4) is unique, resulting in"pull[e]t".
Example 3
Input: labels = ["File", "FILTER"]
Output: ["[File]", "F[ILT]ER"]
Explanation:
"file"is a prefix of"filter"(case-insensitively). Thus,"f","fi", and"fil"appear in both."File"cannot be shortened uniquely, but the full string"File"is unique because"FILTER"does not contain"e"followed by nothing. Wait,"filter"has'e', but"file"has'l'then'e'. The shortest unique substring in"File"is"le"? No:"fil"is in"filter", but"le"is not in"filter"."le"has length 2 and starts at index 2 ("Fi[le]"). However, between"File"and"FILTER","FILT"is not in"File". The exact tie-breaks depend on length first.
Hints
Hint 1
Because the problem asks for the shortest unique substring, you can check candidate substrings by increasing length from 1 up to the length of the string.
Hint 2
Convert all strings to lowercase before checking for substring containment, but perform the bracket insertion on the original string.
Hint 3
For each label, consider pre-filtering other labels or building a combined frequency map / suffix set of all other lowercase strings to speed up containment queries.
Follow-up questions
- How would you optimize this if labels had up to 100,000 strings with average length 20?
- How would you modify your solution if shortcut triggers could only be alphanumeric characters and could not span across whitespace?
- If labels are added or removed dynamically in a live search component, what data structure would you use to support incremental updates?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.