Choose K Letters

mail@pastecode.io avatar
unknown
c_cpp
a year ago
705 B
10
Indexable
Never
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];

    }