Loading content...
You are given an array of strings words. Your task is to identify and extract all characters that are present in every string within the given array, accounting for their frequency of occurrence.
A character should appear in your result as many times as it appears in all strings. In other words, if a character appears twice in each string, it should appear twice in your output. If a character appears three times in one string but only once in another, it contributes only once to the result (the minimum frequency across all strings).
Return the list of shared characters. The result may be returned in any order.
This problem tests your ability to:
The key insight is that for each character, the number of times it can appear in the final answer is limited by the minimum count of that character across all input strings.
words = ["bella", "label", "roller"]["e", "l", "l"]The character 'e' appears at least once in all three strings: "b-e-lla", "lab-e-l", "roll-e-r". The character 'l' appears twice in "bella" (bel-l-a has 2 l's), twice in "label" (l-abe-l has 2 l's), and twice in "roller" (ro-l-l-er has 2 l's). Thus 'l' appears twice in the output. Other characters like 'b', 'a', 'r', 'o' don't appear in ALL strings, so they're excluded.
words = ["cool", "lock", "cook"]["c", "o"]The character 'c' appears once in "cool", once in "lock", and once in "cook" — so it contributes once. The character 'o' appears twice in "cool", once in "lock", and twice in "cook". The minimum is 1, so 'o' appears once. The character 'k' appears in "lock" and "cook" but NOT in "cool", so it's excluded.
words = ["abc", "abc", "abc"]["a", "b", "c"]When all strings are identical, every character from that string appears in the result exactly as many times as it appears in each individual string. Each of 'a', 'b', and 'c' appears exactly once in all strings.
Constraints