Advertisement

Find Common Characters - LeetCode 1002 Solution

Find Common Characters - Complete Solution Guide

Find Common Characters is LeetCode problem 1002, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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.

Detailed Explanation

This problem asks us to identify all characters that are present in *every single string* within a given input array `words`. The critical detail here, which often trips up newcomers, is the 'including duplicates' clause. It's not just about whether a character exists in all strings, but rather, how many times it can *simultaneously* exist in all of them. For instance, if 'l' appears twice in "bella", twice in "label", and twice in "roller", then 'l' is common twice. But if 'o' appears twice in "cool", once in "lock", and twice in "cook", then 'o' is only common once, limited by its single appearance in "lock". We need to collect these characters, respecting their minimal common frequency, and return them as a list.

Solution Approach

The provided solution leverages the idea that if a character is to be common across *all* strings, it *must* at least be present in the very first string. This is our starting point. We first build a frequency map for the characters in `words[0]`. This map then serves as our candidate pool for common characters. For each character identified in this first word, we then iterate through *all* subsequent words in the `words` array. In each subsequent word, we count how many times that specific character appears. We continuously track the *minimum* count found for that character across all words. Once we've checked every word, this `min_count` represents the maximum number of times that character can be considered truly 'common'. Finally, we append this character to our result list `min_count` times, effectively incorporating the duplicate requirement. This process repeats for every unique character initially found in `words[0]`.

Step-by-Step Algorithm

  1. Step 1: Initialize a frequency array (or hash map) to store the minimum frequency of each character (a-z). Initialize all frequencies to a high value (e.g., Integer.MAX_VALUE in Java, INT_MAX in C++).
  2. Step 2: Iterate through each string in the input array.
  3. Step 3: For each string, create a temporary frequency array (or hash map) to count character occurrences within that string.
  4. Step 4: Update the minimum frequency array by comparing the current character frequencies with the minimum frequencies found so far. Use `min()` function to find the minimum frequency for each character across all strings processed so far.
  5. Step 5: After processing all strings, iterate through the minimum frequency array. For each character, append it to the result array as many times as its minimum frequency indicates.
  6. Step 6: Return the result array.

Key Insights

  • The 'bottleneck' frequency: For any given character, its common count across all strings is dictated by its *lowest* frequency in any single string. If 'x' appears 5 times in word A, 2 times in word B, and 8 times in word C, then 'x' is common only 2 times. We must find this minimum across all words for each character.
  • First word as the candidate filter: By initially building a frequency map only for `words[0]`, we automatically filter out any characters that cannot possibly be common to *all* strings. If a character isn't even in the first word, it certainly can't be in every word, saving unnecessary checks.
  • Iterative frequency intersection: Instead of generating full frequency maps for every single word and then merging them, this solution iteratively refines the count for each candidate character. It picks a character from the first word, then successively reduces its `min_count` as it checks its presence in subsequent words. This direct, character-by-character approach elegantly finds the shared minimum frequencies.

Complexity Analysis

Time Complexity: O(n*m*k)

Space Complexity: O(1)

Topics

This problem involves: Array, Hash Table, String.

Companies

Asked at: Tripadvisor.