Design a class that checks whether a word's abbreviation is unique in a dictionary
Company: Google
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
The abbreviation of a word is the concatenation of its first letter, the number of characters between the first and last letter, and its last letter. If the word has only one or two characters, it is an abbreviation of itself.
For example, `dog` becomes `d1g` because there is one letter between `d` and `g`. `internationalization` becomes `i18n` because there are 18 letters between `i` and `n`. `it` stays `it` because any word with only two characters is an abbreviation of itself.
Given a `dictionary` of words, determine whether a given word's abbreviation is **unique** in the dictionary. A word's abbreviation is unique if no other word from the dictionary has the same abbreviation, or if all words in the dictionary with the same abbreviation are identical to the given word.
Implement the class `ValidWordAbbr`:
- `ValidWordAbbr(dictionary: List[str])` initializes the object with the dictionary of words.
- `isUnique(word: str) -> bool` returns `true` if no word in the dictionary has the same abbreviation as the given word, or if all words in the dictionary with the same abbreviation are the same as the given word. Otherwise it returns `false`.
Example 1:
```text
Input:
["ValidWordAbbr", "isUnique", "isUnique", "isUnique", "isUnique"]
[[["deer", "door", "cake", "card"]], ["dear"], ["cart"], ["cane"], ["make"]]
Output: [null, false, true, false, true]
```
The dictionary abbreviates to `d2r` for `deer` and `door`, `c2e` for `cake`, and `c2d` for `card`. `dear` gives `d2r`, which two other words share, so it returns false. `cart` gives `c2t`, which no dictionary word has, so it returns true. `cane` gives `c2e`, which belongs to the different word `cake`, so it returns false. `make` gives `m2e`, which is absent, so it returns true.
Example 2:
```text
Input:
["ValidWordAbbr", "isUnique", "isUnique", "isUnique"]
[[["deer", "door", "cake", "card"]], ["deer"], ["door"], ["cake"]]
Output: [null, false, false, true]
```
`deer` and `door` both give `d2r`, and the dictionary holds two different words with that abbreviation, so both calls return false even though each word is in the dictionary. `cake` gives `c2e`, and the only dictionary word with that abbreviation is `cake` itself, so it returns true.
```hint Precompute once
There can be up to 5,000 queries against up to 30,000 words. Decide what the constructor should compute so that each query does a small, fixed amount of work.
```
```hint Watch the equality cases
The dictionary can list the same word twice, and the query word may itself be in the dictionary. Check that whatever you store per abbreviation can tell those cases apart from two different words sharing it.
```
### Constraints & Assumptions
- `1 <= dictionary.length <= 3 * 10^4`
- `1 <= dictionary[i].length <= 20`
- `dictionary[i]` consists of lowercase English letters.
- `1 <= word.length <= 20`
- `word` consists of lowercase English letters.
- At most 5,000 calls are made to `isUnique`.
### Clarifying Questions to Ask
- Can the dictionary contain the same word more than once, and do repeated copies count as different words?
- Is the dictionary fixed after construction, or will words be added or removed later?
- Is memory a concern, or is it fine to keep every distinct dictionary word in memory?
### What a Strong Answer Covers
- The abbreviation rule applied correctly to words of length 1 and 2 as well as longer words, with one helper used for both dictionary words and queries.
- Preprocessing in the constructor so each query is a single lookup, with the time and space cost of both the constructor and `isUnique`.
- What each abbreviation entry stores, and why that gives the right answer when the dictionary repeats a word or contains the query word.
- A check of all four outcomes: abbreviation absent, held only by the query word, held by one different word, and shared by several words.
### Follow-up Questions
- How would you change the design if words can be added to and removed from the dictionary after construction?
- The dictionary grows to millions of words. How much memory does your map need, and can you put an upper bound on it?
- If the abbreviation kept the first two letters instead of one, which parts of your code would change?
Overview: Design a class that preprocesses a dictionary and answers whether a word's abbreviation (first letter, count of middle letters, last letter) is unique among the dictionary's words. It tests preprocessing for fast repeated queries, careful handling of repeated and identical words, and complexity analysis.