Skip to main content
LeetCode 288, Medium. Topics: Design, Array, Hash Table, String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 18 parametrized pytest cases, and a playground notebook:

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 a dictionary of words.
  • boolean isUnique(string word) Returns true if either of the following conditions are met (otherwise returns false):
    • There is no word in dictionary whose abbreviation is equal to word’s abbreviation.
    • For any word in dictionary whose abbreviation is equal to word’s abbreviation, that word and word are the same.

Examples

Constraints

  • 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 5000 calls will be made to isUnique.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026