288. Unique Word Abbreviation - cocoder39/coco39_LC GitHub Wiki

288. Unique Word Abbreviation

class ValidWordAbbr {
public:
    ValidWordAbbr(vector<string> &dictionary) {
        for (auto &word : dictionary) {
            string abbr = to_abbr(word);
            map[abbr].insert(word);
        }
    }

    bool isUnique(string word) {
        string abbr = to_abbr(word);
        //return map.find(abbr) == map.end() || (map[abbr].find(word) != map[abbr].end() && map[abbr].size() == 1);
        return mp[abbr].count(word) == mp[abbr].size(); //if abbr is not in map, it would be inserted into map and return the iterator
    }
private:
    unordered_map<string, unordered_set<string>> map;
    
    string to_abbr(string& word) {
        int len = word.length();
        string abbr = len <= 2 ? word : word[0] + to_string(len - 2) + word[len - 1];
        return abbr;
    }
};
⚠️ **GitHub.com Fallback** ⚠️