Problem
The abbreviation of a word is a concatenation of its first letter, the number of characters between the first and last letter, and its last letter. If a word has only two characters, then it is an abbreviation of itself.
For example:
dog --> d1g
because there is one letter between the first letter'd'
and the last letter'g'
.internationalization --> i18n
because there are 18 letters between the first letter'i'
and the last letter'n'
.it --> it
because any word with only two characters is an abbreviation of itself.
Implement the ValidWordAbbr
class:
ValidWordAbbr(String[] dictionary)
Initializes the object with adictionary
of words.boolean isUnique(string word)
Returnstrue
if either of the following conditions are met (otherwise returnsfalse
):- There is no word in
dictionary
whose abbreviation is equal toword
‘s abbreviation. - For any word in
dictionary
whose abbreviation is equal toword
‘s abbreviation, that word andword
are the same.
- There is no word in
Examples
Example 1:
|
|
Solution
Method 1 - Using Hashmap
To solve this problem, we need to handle the abbreviation of words in a given dictionary and check if a given word’s abbreviation is unique based on certain conditions.
The abbreviation of a word is formed by:
- The first letter.
- The number of characters between the first and last letter.
- The last letter.
For example, the abbreviation of "dog"
is "d1g"
, and for "internationalization"
it is "i18n"
. Words with exactly two letters are abbreviated to the word itself.
Approach
- Store Abbreviations:
- Use a dictionary (or hashmap) to map abbreviations to the set of words that have that abbreviation.
- Initialization:
- Create a
ValidWordAbbr
class that initializes the dictionary with the provided list of words.
- Create a
- Check Uniqueness:
- A method
isUnique
that checks for the uniqueness based on the following conditions:- If the abbreviation of the given word is not in the dictionary, return
True
. - If it is in the dictionary, check if the abbreviation maps to the exact same word or if there are no other words with that abbreviation.
- If the abbreviation of the given word is not in the dictionary, return
- A method
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
for initializing the dictionary withn
words.O(1)
for checking the uniqueness of a word. - 🧺 Space complexity:
O(n)
for storing the abbreviations.