Find shortest unique substring per word
Company: Affirm
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Shortest unique substring for each word
You are given an array of **distinct** strings `words`.
For each word `w` in `words`, find a **non-empty contiguous substring** of `w` that **does not appear as a substring in any other word** in the array.
Return, for every word, the **shortest** such substring.
### Tie-breaking
If a word has multiple valid substrings with the same minimal length:
1. Choose the one with the **smallest starting index** in the word.
2. If there is still a tie, choose the **lexicographically smallest** substring.
You may assume that for every word, at least one such substring exists.
### Example
**Input**:
```text
['cheapair', 'cheapoair', 'peloton', 'pelican']
```
**Output**:
```text
{
'cheapair': 'pa',
'cheapoair': 'po',
'peloton': 't',
'pelican': 'li'
}
```
### Constraints (typical interview ranges)
- `1 <= len(words) <= 2e3`
- `1 <= len(words[i]) <= 2e3`
- Total characters across all words `<= 2e4`
- Lowercase English letters (you may generalize if desired)
Quick Answer: This question evaluates string-processing and algorithmic problem-solving skills, focusing on identifying substrings unique to each word while handling tie-breaking rules like starting index and lexicographic order and considering time/space trade-offs.
For each word, return its shortest substring absent from all other words. Ties use lexicographic order; return an empty string if none exists.
Constraints
- Inputs are provided as Python literals compatible with the function signature.
- Return a deterministic value exactly matching the requested output.
Examples
Input: (['cab', 'ad', 'bad', 'c'],)
Expected Output: ['ab', '', 'ba', '']
Explanation: Includes a word with no unique substring.
Input: (['apple', 'apply', 'ape'],)
Expected Output: ['le', 'y', 'pe']
Explanation: Tie-breaking.
Input: (['same', 'same'],)
Expected Output: ['', '']
Explanation: Duplicates.
Hints
- Start with a direct data structure representation.
- Handle edge cases before the main loop.