/288. Unique Word Abbreviation

288. Unique Word Abbreviation

Medium
Arrays27.4% acceptance

Given a list of distinct strings, implement a class that can check if a given strings abbreviation is unique among the list. The abbreviation for a string is defined as the first character, followed by the number of characters between the first and last character, and then the last character. If the string length is less than or equal to 2, the abbreviation is the string itself. The class should support two operations: initialization with the list of strings, and checking if a strings abbreviation is unique.

Example 1

Input: word_list = [apple, apply, ape] checker = UniqueAbbreviationChecker(word_list) checker.is_unique(apricot)

Output: True

Explanation: The abbreviation for apricot is a5t. No other word in the list has this abbreviation.

Example 2

Input: word_list = [door, deer, cake] checker = UniqueAbbreviationChecker(word_list) checker.is_unique(dare)

Output: False

Explanation: The abbreviation for dare is d2e, which matches deer.

Constraints

  • 1 <= len(word_list) <= 10^4
  • 1 <= len(query_word) <= 100
  • All words in word_list are distinct
  • All words contain only lowercase English letters
Python (current runtime)

Case 1

Input: word_list = ['table', 'tablet', 'tab'] checker = UniqueAbbreviationChecker(word_list) checker.is_unique('tale')

Expected: True

Case 2

Input: word_list = ['mouse', 'muse', 'moose'] checker = UniqueAbbreviationChecker(word_list) checker.is_unique('muse')

Expected: False

Case 3

Input: word_list = ['pen', 'pan', 'pin'] checker = UniqueAbbreviationChecker(word_list) checker.is_unique('pen')

Expected: True