587/670

587. Find Common Characters

Easy

You are given an array words of strings made of lowercase English letters. A letter is shared when it shows up in every word — and it counts once per shared occurrence: if a letter appears at least twice in every single word, it contributes two copies to the answer.

Return the list of shared characters (duplicates included) in alphabetical order.

For example, with ["bella", "label", "roller"], the letter e appears in all three words once, and l appears in all three at least twice, so the answer is ["e", "l", "l"].

Example 1:

Input: words = ["bella", "label", "roller"]

Output: ["e", "l", "l"]

Explanation: Counting per word: e appears 1, 1, and 1 time; l appears 2, 2, and 2 times. Everything else misses at least one word (b is absent from roller, r is absent from bella). Alphabetical order gives e, l, l.

Example 2:

Input: words = ["cool", "lock", "cook"]

Output: ["c", "o"]

Explanation: c appears in all three words, and o appears in all three — but o only once in lock, so it contributes a single copy despite the double o in cool and cook.

Constraints:

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 100
  • words[i] consists of lowercase English letters only.

Hints:

How many copies of the letter x belong in the answer? Exactly the minimum, over all words, of how many times x appears in that word.

The alphabet has only 26 letters, so keep a running array of 26 minimum counts: count each word into a fresh 26-slot array, then fold it into the running array with per-letter min. Emit letter i that many times, scanning a → z for alphabetical order.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: words = ["bella", "label", "roller"]

Expected output: ["e", "l", "l"]