IBM OA Coding Interview: Filter Words by Letters

17 Views
No Comments

Implement the function filter_words(words, letters) which takes as input:

  • a list of strings words, with the words to filter
  • a string letters, used to filter the words

Your filter_words function should return a list of strings from words that contain at least one letter in letters.

For example:

words = ['the', 'dog', 'got', 'a', 'bone']
letters = 'ae'

The returned list should preserve the original order of words.

This problem asks you to filter a list of words and keep only those that contain at least one character from a given set of letters, while preserving the original order. A clean solution is to scan each word and check whether any character appears in the filter set; once a match is found, include that word and move on. Converting letters to a set makes membership checks efficient and keeps the implementation straightforward.

END
 0