Choose K Letters

 avatar
unknown
c_cpp
2 years ago
705 B
12
Indexable
int solution (vector<string> &s, int k){
        
        map<set<char>, int> m;
        priority_queue<pair<int, char>> pq;
        unordered_map<char, int> freq;

        for(string str: s){
            set<char> charSet;

            for(char c: str){
                charSet.insert(c);
            }

            ++m[charSet];

            for(char c: charSet)
            ++freq[c];
        }

        for(auto [c, val]: freq)
        pq.push({val, c});

        set<char> finalSet;

        for(int i=0; !pq.empty() && i<k; ++i){
            auto p = pq.top();
            pq.pop();
            finalSet.insert(p.second);
        }

        return m[finalSet];

    }
Editor is loading...