Find Common Characters - Problem

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.

For example, if a character occurs 3 times in each string and 2 times in another string, you should include that character 2 times in the final answer (the minimum occurrence across all strings).

Input & Output

Example 1 — Basic Case
$ Input: words = ["bella","label","roller"]
Output: ["e","l","l"]
💡 Note: Character 'e' appears once in each word, 'l' appears twice in each word, so result is ["e", "l", "l"]
Example 2 — Different Frequencies
$ Input: words = ["cool","lock","cook"]
Output: ["c","o"]
💡 Note: Character 'c' appears once in each word, 'o' appears twice in 'cool' and 'cook' but once in 'lock', so we take minimum count of 1
Example 3 — No Common Characters
$ Input: words = ["abc","def","ghi"]
Output: []
💡 Note: No characters appear in all three words, so result is empty array

Constraints

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

Visualization

Tap to expand
Find Common Characters INPUT words = ["bella","label","roller"] "bella" "label" "roller" Character Frequency: bella: b=1,e=1,l=2,a=1 label: l=2,a=1,b=1,e=1 roller: r=2,o=1,l=2,e=1 3 strings to process Find chars in ALL strings ALGORITHM STEPS 1 Initialize Count Count chars in first word minCount[26] from "bella" 2 Iterate Words Count chars in each word currCount[26] for each word 3 Take Minimum min(minCount, currCount) e: min(1,1,1)=1 l: min(2,2,2)=2 4 Build Result Add chars with count > 0 result.push('e','l','l') FINAL RESULT Common Characters Found: "e" "l" "l" ["e","l","l"] Verification: - 'e' appears 1x in all - 'l' appears 2x in all - Other chars not common OK Key Insight: Use frequency counting with 26-element arrays. For each character, track the MINIMUM count across all words. A character appears in result only if its minimum count is greater than 0. Time: O(n*m) where n = number of words, m = average word length. Space: O(1) using fixed 26-char arrays. TutorialsPoint - Find Common Characters | Optimal Solution
Asked in
Google 15 Amazon 12 Facebook 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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