• + 0 comments

    My best C++ solution using Unordered_map. It may not be safe and I think I'm breaking some rules, but it was the closest I could get. It passed all tests.

    .

    vector<int> contacts(vector<vector<string>> queries) {
        
        vector<int> results;
        unordered_map<string, int> chars;
        
        for(vector<string>& query : queries) {
            
            string& cmd      = query[0];    
            string& cmdValue = query[1];
            
            if(cmd == "add") {
                
                string name = "";
                for(char& c : cmdValue) {
                    name.push_back(c);
                    chars[name]++;
                }
                
                continue;
                
            }
            
            results.push_back(chars[cmdValue]);        
            
        }
        
        return results;
    }