Skip to main content

Find Common Characters - Solution & Explanation

EasyArrayHash TableString16 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

Problem Statement

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.

 

Example 1:

Input: words = ["bella","label","roller"]
Output: ["e","l","l"]

Example 2:

Input: words = ["cool","lock","cook"]
Output: ["c","o"]

 

Constraints:

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

Approach Overview

Problem Overview: You are given an array of lowercase strings. The goal is to return all characters that appear in every word, including duplicates. If a character appears twice in all strings, it must appear twice in the result.

Approach 1: Counting Characters with Maps (Time: O(n * k), Space: O(26))

This approach uses a hash map to track character frequencies for each word. Start by counting characters in the first word. Then iterate through the remaining words and update the counts by taking the minimum frequency for each character. If a character appears fewer times in another word, reduce the stored count accordingly. The final map represents the characters common across all words. This approach works well when using a hash table abstraction because it keeps the logic simple and easy to reason about.

After processing all strings, iterate through the map and append each character to the result as many times as its stored frequency. The key insight is that the minimum frequency of each character across all words determines how many times it appears in the answer.

Approach 2: Utilizing Frequency Arrays Directly (Time: O(n * k), Space: O(26))

Since all characters are lowercase English letters, a fixed-size frequency array of length 26 replaces the hash map. First compute the frequency array for the first string. Then iterate through the remaining words and compute a temporary frequency array for each word. Update the global array by taking the minimum value for every index.

This method avoids hash lookups and relies on direct index access, making it slightly faster in practice. For each character position i, the stored value represents the minimum number of times that character appears in every word. After processing all strings, iterate through the array and append characters to the result accordingly.

This technique is common in problems involving arrays and string frequency counting where the character set is small and fixed. Using arrays keeps memory predictable and operations constant time.

Recommended for interviews: The frequency array approach is usually preferred. It demonstrates awareness of constraints (only 26 lowercase letters) and reduces overhead compared to hash maps. The map-based approach still shows solid understanding of frequency counting and works well if the character set were larger or unknown.

Approach 1: Approach 1: Counting Characters with Maps

In this approach, we will use a hash map (or dictionary) to store the frequency of each character for each word. We then update a common frequency count table that holds the minimum frequency of each character across all words. This ensures that only characters existing in all words are recorded.

This C solution initializes two arrays of size 26 (for each letter of the alphabet) to count occurrences. It iterates over each word, updates character counts, and then calculates the minimum count for each character.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N*K) where N is the number of words and K is the average length of the words. Space Complexity: O(1) since the space does not scale with input size.

Try this approach in the editor →

Approach 2: Approach 2: Utilizing Frequency Arrays Directly

We can alternatively use direct character arrays to represent frequencies and update these arrays with each subsequent word processed. Starting with the first word's character frequencies, we iteratively compute the minimum with the rest.

In this C implementation, we use frequency arrays to compute the minimum occurrence of each character across all words. Frequency is updated per word using a temporary array and merged via a minimum operation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N*K). Space Complexity: O(1) for constant sized arrays.

Try this approach in the editor →

Approach 3: Counting

We use an array cnt of length 26 to record the minimum number of times each character appears in all strings. Finally, we traverse the cnt array and add characters with a count greater than 0 to the answer.

The time complexity is O(n sum w_i), and the space complexity is O(|\Sigma|). Here, n is the length of the string array words, w_i is the length of the i-th string in the array words, and |\Sigma| is the size of the character set, which is 26 in this problem.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Counting Characters with Maps

Time Complexity: O(N*K) where N is the number of words and K is the average length of the words. Space Complexity: O(1) since the space does not scale with input size.

Approach 2: Utilizing Frequency Arrays Directly

Time Complexity: O(N*K). Space Complexity: O(1) for constant sized arrays.

Counting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Counting Characters with MapsO(n * k)O(26)General solution when using hash maps or when character set size may vary
Frequency Arrays DirectlyO(n * k)O(26)Best choice when characters are limited to lowercase letters and constant-time indexing is preferred

Video Solution

LeetCode Find Common Characters Solution Explained - Java • Nick White • 22,057 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find Common Characters easy or hard?
Find Common Characters is classified as an Easy problem. The challenge mainly involves tracking minimum character frequencies across multiple strings using arrays or hash maps.
Find Common Characters Python/Java solution
In Python and Java, the common solution uses either a dictionary/map or an integer array of size 26. For each word, compute character frequencies and update the global minimum counts. Finally, rebuild the result list using the stored frequencies.
How to solve Find Common Characters in O(n)?
Treat each word as part of a single pass frequency comparison. Compute the character frequency for the first word, then update it with the minimum frequency from every other word. Since each character is processed once per string, the total complexity becomes O(n * k).
What is the best approach for Find Common Characters?
The frequency array approach is typically the best solution. Because the problem only uses lowercase English letters, you can maintain a 26-length array and keep the minimum frequency across all strings. This results in O(n * k) time and O(26) space while avoiding hash map overhead.
Is Find Common Characters asked at Google/Amazon/Meta?
Variants of character frequency and string intersection problems appear frequently in interviews at companies like Amazon, Google, and Meta. They test understanding of hash tables, counting techniques, and efficient string processing.
What data structure is used in Find Common Characters?
The main data structures are hash maps or fixed-size frequency arrays. A hash map stores character counts dynamically, while a 26-length array provides faster constant-time indexing for lowercase letters.
What is the time complexity of Find Common Characters?
The time complexity is O(n * k), where n is the number of words and k is the average length of each word. Each string is scanned once to count character frequencies, and only 26 characters are compared when updating the global minimum counts.

Ready to solve this problem?

Practice Find Common Characters with our built-in code editor and test cases.

Practice on FleetCode