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:
words = ["bella","label","roller"]
Output: ["e","l","l"]
words = ["cool","lock","cook"]
Output: ["c","o"]
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i]
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding common characters in a list of words involves checking every possible character across all the words. We will start by looking at the first word and comparing each character of this word with all the other words to see if it exists in all of them. This is like an exhaustive search, ensuring no possibility is missed.
Here's how the algorithm would work step-by-step:
def find_common_characters_brute_force(words):
first_word = words[0]
common_characters = []
for char_index in range(len(first_word)):
character = first_word[char_index]
is_common = True
# Check if the character is present in all other words
for word_index in range(1, len(words)):
if character not in words[word_index]:
is_common = False
break
if is_common:
# If char is common, count occurrences in each word
min_occurrences = float('inf')
for word in words:
occurrences = word.count(character)
min_occurrences = min(min_occurrences, occurrences)
# Add the character to the result
# based on minimum occurrences.
for _ in range(min_occurrences):
common_characters.append(character)
# Remove the character from the words so it isn't double counted
for word_index in range(len(words)):
words[word_index] = words[word_index].replace(character, '', min_occurrences)
return common_characters
To efficiently find common characters across multiple words, we'll count how often each letter appears in each word. Then, we identify the letters that appear in all words and, for each such letter, determine the minimum number of times it appears in any of the words.
Here's how the algorithm would work step-by-step:
def find_common_characters(words):
letter_counts = []
for word in words:
word_count = {}
for char in word:
word_count[char] = word_count.get(char, 0) + 1
letter_counts.append(word_count)
# Determine all distinct characters across all words
all_chars = set()
for word_count in letter_counts:
all_chars.update(word_count.keys())
common_characters = []
for character in all_chars:
# Check if the character is present in all words
is_common = True
for word_count in letter_counts:
if character not in word_count:
is_common = False
break
# If the character is in all words, find min count
if is_common:
# Find the minimum frequency of this char
min_frequency = float('inf')
for word_count in letter_counts:
min_frequency = min(min_frequency, word_count[character])
# Add the character to the result the appropriate number of times
for _ in range(min_frequency):
common_characters.append(character)
return common_characters
Case | How to Handle |
---|---|
Input array of strings is null or empty | Return an empty list, as there are no strings to compare for common characters. |
One or more input strings are null or empty | Treat an empty string as having no characters, thus the intersection with it will always be empty. |
Input array contains only one string | Return the unique characters in that single string as a list, since those are common to itself. |
All input strings are identical | Return a list of the unique characters in any one of the identical strings, maintaining their original frequency. |
Input strings contain non-alphabetic characters (numbers, symbols, whitespace) | Include only alphabetic characters in the resulting common characters, ignoring other characters in the strings. |
One string contains a character repeated many times, but another string contains it only once | The character should only appear in the result as many times as it appears in the string with the fewest occurrences of that character. |
Input strings are very large (potential memory issues) | Use a character counting approach (hash map/array) to avoid storing large intermediate strings and optimize space. |
No common characters exist among all input strings | Return an empty list, indicating no commonality. |