You are given a 0-indexed array of strings words and a character x.
Return an array of indices representing the words that contain the character x.
Note that the returned array may be in any order.
Example 1:
Input: words = ["leet","code"], x = "e" Output: [0,1] Explanation: "e" occurs in both words: "leet", and "code". Hence, we return indices 0 and 1.
Example 2:
Input: words = ["abc","bcd","aaaa","cbc"], x = "a" Output: [0,2] Explanation: "a" occurs in "abc", and "aaaa". Hence, we return indices 0 and 2.
Example 3:
Input: words = ["abc","bcd","aaaa","cbc"], x = "z" Output: [] Explanation: "z" does not occur in any of the words. Hence, we return an empty array.
Constraints:
1 <= words.length <= 501 <= words[i].length <= 50x is a lowercase English letter.words[i] consists only 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 involves going through each word in the provided list. For every word, we simply check if the target character is present within that word.
Here's how the algorithm would work step-by-step:
def find_words_containing_character(words, character):
indices_of_matching_words = []
for word_index in range(len(words)):
word = words[word_index]
# Iterate through each character to find match
for char_index in range(len(word)):
if word[char_index] == character:
# Store index because the word contains char
indices_of_matching_words.append(word_index)
break
# Return all of the word indices that match
return indices_of_matching_wordsThe goal is to identify all the words in a list that include a specific character. Instead of complex searching, the best way is to simply check each word individually. This guarantees we find all matching words quickly and accurately.
Here's how the algorithm would work step-by-step:
def find_words_containing_character(words, character):
words_containing_char = []
# Iterate through each word in the input list
for word in words:
# Check if the character is present in the current word
if character in word:
# Add the word to result if the character is found
words_containing_char.append(word)
return words_containing_char| Case | How to Handle |
|---|---|
| words is null or empty | Return an empty list since there are no words to search. |
| char is null or empty string | Return an empty list because no characters are provided to search for. |
| words contains empty strings | An empty string cannot contain a character, so skip it. |
| words contains null strings | Treat null strings as empty strings and skip them. |
| words contains very long strings | The linear scan through each word should still work, but consider potential performance implications for extremely long words. |
| char appears at the beginning of a word. | The character search should correctly identify words that start with the target character. |
| char appears at the end of a word. | The character search should correctly identify words that end with the target character. |
| words contains duplicate strings | The algorithm will correctly identify the index of each occurrence of the duplicate strings if they contain the character; duplicates do not affect correctness. |