Problem Β· Array
Underline Shortest Unique Substrings
Learn this problemProblem statement
Given an array of nonempty strings, transform every string by underlining its shortest substring that occurs in none of the other strings.
- Compare substrings case-insensitively.
- If several shortest substrings qualify, choose the one whose occurrence starts earliest in the current string.
- Preserve each string's original casing and preserve the input order.
- Wrap only the selected occurrence in the literal tags
<u>and</u>. - If a string has no qualifying substring, return it unchanged.
Return the transformed strings.
Function
underlineShortestUniqueSubstrings(strings: String[]) β String[]Examples
Example 1
strings = ["Bird","Cat","Cow","Dog","Wallaby"]return = ["B<u>i</u>rd","Ca<u>t</u>","<u>Co</u>w","Do<u>g</u>","Wa<u>l</u>laby"]For Bird, b also appears in Wallaby, while i appears in no other string, so the earliest shortest choice is i. For Cow, every single letter appears elsewhere, and Co is the earliest qualifying substring of length two. The other strings have the shown unique single-letter choices.
Example 2
strings = ["Rose","rose"]return = ["Rose","rose"]Comparison ignores case, so every substring of either string occurs in the other. Both strings remain unchanged.
Constraints
1 <= strings.length <= 200.1 <= strings[i].length <= 200.- Every input string contains only uppercase or lowercase ASCII letters.
- The inserted underline tags do not participate in comparisons.