LC 1170. Compare Strings by Frequency of the Smallest Character

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
private:
    int f(string s){
        string temp = s;
        sort(temp.begin(),temp.end());
        return count(temp.begin(),temp.end(),temp[0]);
    }
public:
    vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
        vector<int> res;
        vector<int> store_q;
        vector<int> store_w;
        int n = queries.size();
        int m = words.size();
        for(int i=0;i<n;i++)
            store_q.push_back(f(queries[i]));
        for(int i=0;i<m;i++)
            store_w.push_back(f(words[i]));
        for(int i=0;i<n;i++){
            int cnt = 0;
            for(int j=0;j<m;j++){
                if(store_q[i] < store_w[j])
                    cnt++;
            }
            res.push_back(cnt);
        }
        return res;
    }
};


Dicussion Forum