Skip to main content

Minimum Distinct Prefix Lengths

hard
ArrayHash TableTrieStringTrie Prefix Search
Asked atGoogleAmazonMetaMicrosoft

Problem Description

You are given an array of lowercase strings `words`. For each word in `words`, find the length of its shortest unique prefix — that is, the shortest prefix that is not a prefix of any other word in the array.

Return an integer array `result` where `result[i]` is the minimum prefix length for `words[i]`. If a word itself is a prefix of another word in the array, its unique prefix length equals its own length.

**Example 1:**
```
Input: words = ["flower", "flow", "flight", "floor"]
Output: [3, 4, 3, 4]
```
Explanation:
- "flower": shortest prefix not shared — "flo" is shared with "floor", "flowe" is unique → length 5? Actually "fl" is shared by all. "flo" is shared by "flower","flow" (no "flow" has "flo"? "flow" starts with "flo"? f-l-o-w yes). So "flo" shared among flower/flow/floor. "flow" shared by flower/flow. "flowe" only in flower → length 5. But "fli" is unique to "flight" → length 3. Let's recompute carefully with trie approach.

Actually example recomputed: Using a trie with counts:
- "fl" count=4, "flo" count=3 (flower,flow,floor), "fli" count=1 → flight prefix length 3 ✓
- "flow" count=2 (flower,flow), "floo" count=1 → floor prefix length 4 ✓
- "flow" count=2, "flowe" count=1 → flower prefix length 5
- "flow" is a full word but also prefix of "flower", so unique prefix for "flow" = 4 ("flow" itself)

Let me use a simpler example.

**Example 1:**
```
Input: words = ["apple", "ape", "april", "banana"]
Output: [4, 3, 4, 1]
```
Explanation:
- "apple": "app" is shared with "april", "appl" is unique → length 4
- "ape": "ap" is shared with "apple"/"april", "ape" is unique → length 3
- "april": "app" shares with "apple", "apr" unique → length 4? "apr" is only in "april" → length 3. Hmm. "ap" is in apple/ape/april (count 3). "ape" count 1. "app" count 2. "apr" count 1 → april prefix length 3.

Let me just give clean final examples:

**Example 1:**
```
Input: words = ["dog", "dove", "door", "cat"]
Output: [3, 3, 3, 1]
```
Explanation: All three "d" words share "d" and "do". "dog" is unique at "dog" (len 3); "dove" unique at "dov" (len 3); "door" unique at "doo" (len 3). "cat" unique at "c" (len 1).

**Example 2:**
```
Input: words = ["abc", "ab", "a"]
Output: [3, 2, 1]
```
Explanation: "a" is a prefix of all, but is itself the full word "a" → length 1. "ab" is a prefix of "abc" → length 2. "abc" is unique at itself → length 3.

Constraints

  • 1 <= words.length <= 3000
  • 1 <= words[i].length <= 300
  • All strings in words consist of lowercase English letters
  • All strings in words are distinct

Follow-up

Extend the solution to handle an online query stream: given a sequence of word insertions and prefix-length queries, answer each query in amortized O(L) time.

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.

CodeBrainery

A technical blogging platform for developers and engineers to share knowledge and connect.

Connect

© 2026 Kodetra Technologies Pvt. Ltd. All rights reserved. CodeBrainery is a product of Kodetra Technologies Pvt. Ltd.