🎯 Real InterviewEasyAlgorithm ProblemCoding Ready

Filter Words by Letters

Algorithm: 10-15 minutes

stringssetsfilteringlist-comprehensioneasyreal-interview
Tech Company
Updated Jan 4, 2026

Question

Problem

In this problem, you'll be filtering words based on the letters that they contain.

You'll be given an input list of words (all lowercase), along with a set of lowercase letters, and asked to keep only the words that contain at least one letter in the given set.

Implementation:

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. The returned list should maintain the same ordering as in words.

Constraints

  • 1 ≤ len(words) ≤ 10^4
  • 1 ≤ len(word) ≤ 100 for each word in words
  • 1 ≤ len(letters) ≤ 26
  • All characters are lowercase English letters
  • words may contain duplicates

Examples

Example 1

Input:

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

Output:

['the', 'a', 'bone']

Explanation: 'the' contains 'e', 'a' contains 'a', 'bone' contains both 'e' and 'o' (but 'e' matches). 'dog' and 'got' contain neither 'a' nor 'e'.

Example 2

Input:

words = ['hello', 'world', 'python']
letters = 'xyz'

Output:

['python']

Explanation: Only 'python' contains a letter from 'xyz' (the 'y').

Example 3

Input:

words = ['cat', 'bat', 'rat']
letters = 'a'

Output:

['cat', 'bat', 'rat']

Explanation: All words contain 'a', so all are included.

Function Signature

def filter_words(words: list[str], letters: str) -> list[str]:
    """
    Filter words that contain at least one letter from the given set.

    Args:
        words: List of words to filter
        letters: String of letters to check for

    Returns:
        List of words containing at least one letter from letters
    """
    pass

Estimated Time

10-15 minutes

Tags

strings sets filtering list-comprehension easy real-interview


Your Solution

python
Auto-saves every 30s

Try solving the problem first before viewing the solution


0:00time spent