Unique Word Abbreviation

medium design hash map string

Problem

An abbreviation of a word follows the form <first><len(middle)><last>. Build a structure over a dictionary that returns whether a word's abbreviation is unique — i.e., no other dictionary word abbreviates to the same string.

Inputdict=["deer","door","cake","card"]; isUnique("dear")
Outputfalse
"dear"'s abbreviation is "d2r", shared with "deer".

class ValidWordAbbr:
    def __init__(self, dictionary):
        self.m = {}
        for w in set(dictionary):
            key = self._abbr(w)
            self.m.setdefault(key, set()).add(w)
    def _abbr(self, w):
        return w if len(w) <= 2 else f"{w[0]}{len(w)-2}{w[-1]}"
    def isUnique(self, w):
        key = self._abbr(w)
        bucket = self.m.get(key, set())
        return not bucket or (len(bucket) == 1 and w in bucket)
class ValidWordAbbr {
  constructor(dict) {
    this.m = new Map();
    for (const w of new Set(dict)) {
      const key = this._abbr(w);
      if (!this.m.has(key)) this.m.set(key, new Set());
      this.m.get(key).add(w);
    }
  }
  _abbr(w) {
    return w.length <= 2 ? w : w[0] + (w.length - 2) + w[w.length - 1];
  }
  isUnique(w) {
    const b = this.m.get(this._abbr(w)) || new Set();
    return b.size === 0 || (b.size === 1 && b.has(w));
  }
}
class ValidWordAbbr {
    Map> m = new HashMap<>();
    public ValidWordAbbr(String[] dict) {
        for (String w : new HashSet<>(Arrays.asList(dict))) {
            m.computeIfAbsent(abbr(w), k -> new HashSet<>()).add(w);
        }
    }
    String abbr(String w) {
        return w.length() <= 2 ? w : w.charAt(0) + Integer.toString(w.length() - 2) + w.charAt(w.length() - 1);
    }
    public boolean isUnique(String w) {
        Set b = m.getOrDefault(abbr(w), Collections.emptySet());
        return b.isEmpty() || (b.size() == 1 && b.contains(w));
    }
}
class ValidWordAbbr {
    unordered_map> m;
    string abbr(const string& w) {
        return w.size() <= 2 ? w : string(1, w[0]) + to_string(w.size() - 2) + w.back();
    }
public:
    ValidWordAbbr(vector& dict) {
        unordered_set uniq(dict.begin(), dict.end());
        for (auto& w : uniq) m[abbr(w)].insert(w);
    }
    bool isUnique(const string& w) {
        auto it = m.find(abbr(w));
        if (it == m.end()) return true;
        return it->second.size() == 1 && it->second.count(w);
    }
};
Time: O(N · L) build, O(L) query Space: O(N · L)