LeetCode 288: Unique Word Abbreviation Solution

Master LeetCode problem 288 (Unique Word Abbreviation), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

288. Unique Word Abbreviation

Problem Explanation

Explanation

To solve this problem, we can use a hashmap to store the abbreviations of words as keys and the corresponding word as the value. When checking if an abbreviation is unique, we can iterate through the hashmap and check if the abbreviation already exists. If it does, we need to verify if the value associated with that abbreviation is the same as the current word. If it is not the same, then the abbreviation is not unique.

Algorithm:

  1. Create a hashmap to store the abbreviations of words.
  2. For each word in the dictionary:
    • Generate the abbreviation of the word.
    • Check if the abbreviation already exists in the hashmap:
      • If it does, check if the corresponding word is the same as the current word.
      • If it is not the same, then the abbreviation is not unique.
      • If it is the same, continue to the next word.
    • If the abbreviation does not exist in the hashmap, add it to the hashmap.
  3. When checking if an abbreviation is unique, return true if it is unique, false otherwise.

Time Complexity

The time complexity of this approach is O(n) where n is the number of words in the dictionary. This is because we iterate through each word in the dictionary and perform constant time operations.

Space Complexity

The space complexity of this approach is O(n) where n is the number of unique abbreviations stored in the hashmap.

Solution Code

class ValidWordAbbr {
    Map<String, String> map;

    public ValidWordAbbr(String[] dictionary) {
        map = new HashMap<>();
        for (String word : dictionary) {
            String abbr = generateAbbreviation(word);
            if (map.containsKey(abbr) && !map.get(abbr).equals(word)) {
                map.put(abbr, "");
            } else {
                map.put(abbr, word);
            }
        }
    }

    public boolean isUnique(String word) {
        String abbr = generateAbbreviation(word);
        return !map.containsKey(abbr) || map.get(abbr).equals(word);
    }

    private String generateAbbreviation(String word) {
        if (word.length() <= 2) {
            return word;
        }
        return word.charAt(0) + String.valueOf(word.length() - 2) + word.charAt(word.length() - 1);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 288 (Unique Word Abbreviation)?

This page provides optimized solutions for LeetCode problem 288 (Unique Word Abbreviation) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 288 (Unique Word Abbreviation)?

The time complexity for LeetCode 288 (Unique Word Abbreviation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 288 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 288 in Java, C++, or Python.

Back to LeetCode Solutions