Categories:

Tags:



Problem

Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] consists of lowercase English letters.

Solution

The problem Find Common Characters can be solved by counting occurrences of all English letters in each given strings and then finding the minimum occurrence of each letters.

Implementation

static const int fast_io = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    return 0;
}();

class Solution
{
  public:
    vector<string> commonChars(vector<string> &words)
    {
        vector<int> min_count(26, INT_MAX);
        for (int i = 0; i < words.size(); i++)
        {
            int cur_count[26] = {};
            for (char ch : words[i])
                cur_count[ch - 'a'] += 1;

            for (int j = 0; j < 26; j++)
                min_count[j] = min(min_count[j], cur_count[j]);
        }

        vector<string> ret;
        for (int i = 0; i < 26; i++)
            for (int j = 0; j < min_count[i]; j++)
                ret.push_back(string(1, 'a' + i));

        return ret;
    }
};