Short C++ solution using One unordered_map<String,int>


  • 0
    F
    class ValidWordAbbr {
    private:
    unordered_map<string,int> preprocess;
    public:
        ValidWordAbbr(vector<string> &dictionary) {
            for (string& str : dictionary) {
                string abbrevieated = *str.begin() + to_string(str.size()) + *str.rbegin();
                preprocess[abbrevieated]++;
                preprocess[str]++;
            }
        }
        bool isUnique(string word) {
            string abbrev =  *word.begin() + to_string(word.size()) + *word.rbegin();
            return (preprocess[abbrev] < 1 || (preprocess[abbrev] - preprocess[word]) == 0 );
        }
    };
    

Log in to reply
 

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