Simple C++11 solution using single HashTable


  • 0
    S
    class ValidWordAbbr {
    public:
        ValidWordAbbr(vector<string> &dictionary) {
            for (auto& w : dictionary) {++map[mapstr(w)]; ++map['#'+w];}
        }
    
        bool isUnique(string word) {
            return (map['#'+word]==map[mapstr(word)]);
        }
    private:
        unordered_map<string, int> map;
        string mapstr(const string& w) {
            return (w.size()<=2) ? w : (w[0]+to_string(w.size()-2)+w[w.size()-1]);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.