7 lines concise and easy understand c++ solution


  • 3
    A
    class ValidWordAbbr {
    public:
        ValidWordAbbr(vector<string> &dictionary) {
            for(auto word : dictionary){
                string tmp = word[0] + to_string(word.size()) + word[word.size() - 1];
                mp[tmp].insert(word);
            }
        }
    
        bool isUnique(string word) {
            string tmp = word[0] + to_string(word.size()) + word[word.size() - 1];
            return mp[tmp].count(word) == mp[tmp].size();
        }
    private:
        unordered_map<string, unordered_set<string>> mp;
    };

  • 0
    S

    Can you explain what return mp[tmp].count(word) == mp[tmp].size(); means?


  • 0
    S

    In isUnique() method, before calling mp[tmp].count(word) don't you need to check if mp[tmp] exist?


  • 0
    A

    the mp[tmp].count(word) means that the number of string with some abbreviation.


  • 0
    A

    if mp[tmp] not existed, mp[tmp].size() equal zreo


  • 0
    S

    Won't affect the submission correctness, but you need to append "word.size() - 2" between the first and last characters.


Log in to reply
 

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