---
# System prepended metadata

title: LC 1002. Find Common Characters
tags: [easy, leedcode, c++]

---

# LC 1002. Find Common Characters

### [Problem link](https://leetcode.com/problems/find-common-characters/)

###### tags: `leedcode` `easy` `c++`

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

**Example 1:** 
```
Input: words = ["bella","label","roller"]
Output: ["e","l","l"]
```
**Example 2:** 
```
Input: words = ["cool","lock","cook"]
Output: ["c","o"]
```

 **Constraints:** 

- <code>1 <= words.length <= 100</code>
- <code>1 <= words[i].length <= 100</code>
- <code>words[i]</code> consists of lowercase English letters.

## Solution 1
#### C++
```cpp=
class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
        vector<string> res;
        vector<int> common(26, INT_MAX);

        for (string word: words) {
            vector<int> cnt(26, 0);
            for (auto c: word) {
                cnt[c - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                common[i] = min(common[i], cnt[i]);
            }
        }
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < common[i]; j++) {
                res.push_back(string(1, i + 'a'));
            }
        }
        return res;
    }
};
```

>### Complexity
>m =  words.length
>n = sum(words[i].length)
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(mn)           | O(mn)            |

## Note
x
