Remove Stopwords
Remove Stopwords
Given a list of tokens and a list of stopwords, remove all tokens that appear in the stopword list.
Examples
Input: tokens = ["this", "is", "a", "test"], stopwords = ["is", "a"]
Output: ["this", "test"]
Input: tokens = ["hello", "world"], stopwords = ["the", "and"]
Output: ["hello", "world"]
Input: tokens = ["a", "an", "the"], stopwords = ["a", "an", "the"]
Output: []
Hint 1
Convert stopwords to a set for O(1) lookup, then filter tokens using list comprehension.
Requirements
- Treat comparisons as case-sensitive
- Preserve the original order of non-stopword tokens
- Do not modify the input lists in-place
- Return a new list
Constraints
- 0 ≤ len(tokens) ≤ 10,000
- 0 ≤ len(stopwords) ≤ 5,000
- Python only
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Remove Stopwords
Remove Stopwords
Given a list of tokens and a list of stopwords, remove all tokens that appear in the stopword list.
Examples
Input: tokens = ["this", "is", "a", "test"], stopwords = ["is", "a"]
Output: ["this", "test"]
Input: tokens = ["hello", "world"], stopwords = ["the", "and"]
Output: ["hello", "world"]
Input: tokens = ["a", "an", "the"], stopwords = ["a", "an", "the"]
Output: []
Hint 1
Convert stopwords to a set for O(1) lookup, then filter tokens using list comprehension.
Requirements
- Treat comparisons as case-sensitive
- Preserve the original order of non-stopword tokens
- Do not modify the input lists in-place
- Return a new list
Constraints
- 0 ≤ len(tokens) ≤ 10,000
- 0 ≤ len(stopwords) ≤ 5,000
- Python only
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array