LC 1002. Find Common Characters

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
public:
    vector<string> commonChars(vector<string>& A) {
        int n = A.size();
        vector<int> f(26,0);
        for(int i=0;i<26;i++)
            f[i] = count(A[0].begin(),A[0].end(),i+'a');
        for(int i=1;i<n;i++)
            for(int j=0;j<26;j++){
                int f_chr = count(A[i].begin(),A[i].end(),j+'a');
                f[j] = min(f[j],f_chr);
            }
        vector<string> res;
        for(int i=0;i<26;i++){
            while(f[i]--){
                string str;
                str.push_back(i+'a');
                res.push_back(str);
            }
        }
        return res;
    }
};


Dicussion Forum