Skip to content

Latest commit

 

History

History
53 lines (43 loc) · 1.22 KB

File metadata and controls

53 lines (43 loc) · 1.22 KB

Find Common Characters

https://leetcode.com/problems/find-common-characters

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.

Approach

    vector<string> commonChars(vector<string>& words) {
        std::vector<int> common(26, INT_MAX);
        for (const auto& word : words)
        {
            std::vector<int> freq(26, 0);
            for (char ch : word)
            {
                freq[ch - 'a']++;
            }

            for (int i = 0; i < 26; ++i)
            {
                common[i] = std::min(freq[i], common[i]);
            }
        }

        std::vector<std::string> ans;
        for (int i = 0; i < 26; ++i)
        {
            for (int j = 0; j < common[i]; ++j)
            {
                ans.emplace_back(std::string(1, i + 'a'));
            }
        }
        return ans;
    }
var commonChars = function(words) {
    let ans = [];

    for (const ch of words[0])
    {
        if (words.every((word) => word.includes(ch))) {
            ans.push(ch);
            words = words.map((word) => word.replace(ch, ""));
        }
    }
    return ans;
};