LeetCode solutions

1044. Find Common Characters

My accepted C++ solution to LeetCode problem 1044, Find Common Characters, running in 0ms.

  • Difficulty: Easy
  • C++
  • Runtime 0ms
  • Memory 12.6MB

Read the problem on LeetCode View on GitHub

C++

Accepted on LeetCode — runtime 0ms, memory 12.6MB, accepted 2025-12-27.

cpp
class Solution {
    public:
    vector<string> commonChars(vector<string>& words) {
        vector<int> minFreq(26, INT_MAX);
        for (auto& word : words) {
            vector<int> freq(26, 0);
            for (char c : word) freq[c - 'a']++;
            for (int i = 0; i < 26; i++) minFreq[i] = min(minFreq[i], freq[i]);
        }
        vector<string> res;
        for (int i = 0; i < 26; i++) {
            while (minFreq[i]-- > 0) res.push_back(string(1, 'a' + i));
        }
        return res;
    }
};

Source