Find Common Characters - Problem
Find Common Characters

Given an array of strings words, your task is to find all characters that appear in every single string. The catch? You need to include duplicates! If a character appears twice in every string, it should appear twice in your result.

Goal: Return an array containing all characters that show up in all strings within the words array (including duplicates).

Example: If you have ["bella", "label", "roller"], the characters that appear in all three strings are ['e', 'l', 'l']. Notice how 'l' appears twice because it appears at least twice in each string.

You may return the answer in any order.

Input & Output

example_1.py โ€” Basic case with common characters
$ Input: ["bella", "label", "roller"]
โ€บ Output: ["e", "l", "l"]
๐Ÿ’ก Note: 'e' appears once in all words. 'l' appears at least twice in all words, so we include it twice. 'b' and 'a' don't appear in 'roller', and 'r', 'o' don't appear in other words.
example_2.py โ€” No common characters
$ Input: ["abc", "def", "ghi"]
โ€บ Output: []
๐Ÿ’ก Note: No character appears in all three words, so the result is an empty array.
example_3.py โ€” Single word case
$ Input: ["hello"]
โ€บ Output: ["h", "e", "l", "l", "o"]
๐Ÿ’ก Note: With only one word, all characters in that word are 'common' to all words in the array. The 'l' appears twice, so it's included twice in the result.

Visualization

Tap to expand
Character Frequency Intersection Visualizationbellab:1e:1l:2a:1labell:2a:1b:1e:1rollerr:2o:1l:2e:1Intersection Resultโœ“ e: min(1,1,1) = 1โœ“ l: min(2,2,2) = 2โœ— b,a,r,o: not in allAlgorithm Steps:1. Initialize common_count = {b:1, e:1, l:2, a:1} from "bella"2. Process "label": common_count = {b:min(1,1), e:min(1,1), l:min(2,2), a:min(1,1)} = {b:1, e:1, l:2, a:1}3. Process "roller": common_count = {b:min(1,0), e:min(1,1), l:min(2,2), a:min(1,0)} = {b:0, e:1, l:2, a:0}4. Remove zero counts: {e:1, l:2}5. Build result: ["e", "l", "l"]Final Answer: ["e", "l", "l"]
Understanding the Visualization
1
Start with first recipe
bella: b(1), e(1), l(2), a(1) - this gives us our initial 'shopping list'
2
Check second recipe
label: l(2), a(1), b(1), e(1) - all ingredients still available, quantities unchanged
3
Check third recipe
roller: r(2), o(1), l(2), e(1) - b and a are missing! Update our list
4
Final shopping list
Only e(1) and l(2) survived all recipes - these are our common characters
Key Takeaway
๐ŸŽฏ Key Insight: The frequency of any common character is limited by the word that contains the fewest occurrences of that character. Using a hash table to track minimum counts across all words gives us an optimal O(nร—m) solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m)

n words each with m characters, but we process optimally in one pass

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Only one frequency map storing k unique characters from first word

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค words.length โ‰ค 100
  • 1 โ‰ค words[i].length โ‰ค 100
  • words[i] consists of lowercase English letters only
  • Important: Duplicates matter - if a character appears multiple times in every word, include it multiple times in the result
Asked in
Amazon 45 Google 38 Microsoft 32 Apple 28
52.0K Views
Medium Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen