Problem Description
Given an array of strings, the goal is to return an array containing all the characters that appear in every string in the input array (including duplicates). For example, if a character appears twice in every string, it should appear twice in the output.
Key Insights
- Use frequency counting for characters.
- Determine the minimum count for each character across all words.
- Use the minimum frequency to determine how many times a character appears in every string.
- The order of the characters in the resulting list does not matter.
Space and Time Complexity
Time Complexity: O(m * n) where m is the number of words and n is the average length of the words. Space Complexity: O(1) since the count arrays are of fixed size (26 for the alphabet).
Solution
We maintain a global frequency count for each of the 26 lowercase English letters. Initially, set the count for each letter to a very high number (or use the frequency of the first word). For each subsequent word, compute the frequency of each letter and update the global frequency by taking the minimum frequency encountered so far for each letter. Finally, traverse the frequency array and add each character to the result the number of times it appears (i.e., its minimum frequency across all words).