Filter Words Containing a Letter
Problem statement
Given a list of words words and one letter letter, return every word that contains that letter at least once.
For this exercise, assume the words and letter use lowercase English letters. Keep the original word order and keep repeated words. Include a matching word only once for each occurrence in the input, even when the letter appears several times in that word.
Function
filterWords(words: String[], letter: String) → String[]Examples
Example 1
words = ["apple","pear","banana","kiwi"]letter = "a"return = ["apple","pear","banana"]apple, pear, and banana contain a. kiwi does not. The matching words keep their input order.
Example 2
words = ["","book","book","sky"]letter = "o"return = ["book","book"]Both occurrences of book are retained. Its two o letters do not add extra copies. The empty word and sky do not match.
Constraints
0 <= words.length <= 10000 <= words[i].length <= 100letter.length == 1Every word and letter contains only lowercase English letters.