Fast Stopword Filter

Easy ⏱ 8 min 72% acceptance ★★★★★ 4.8
Write remove_stopwords(words, stopwords) that returns the input list of words with every stopword removed, preserving the original order of the survivors. Convert stopwords to a set first so each lookup is O(1) instead of scanning a list.

Examples

Example 1
Input
remove_stopwords(['the', 'cat', 'on', 'mat'], ['the', 'on', 'a'])
Output
['cat', 'mat']
Explanation

the and on are stopwords; order of the rest is preserved.

Constraints

  • Preserve original order.
  • Convert stopwords to a set exactly once.

Topics

Setslookup performance

Companies

GoogleAmazonTCS

Hints

Hint 1

Membership tests on a set are constant time.

Hint 2

A simple loop or comprehension with "not in" finishes it.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.