Problem · Array
Spam Text Classification
Learn this problemProblem statement
You are given:
texts[n]: an array of text strings to classifyspamWords[k]: an array of words considered spam indicators
Classify each text as "spam" or "not_spam" using these rules:
- A text is
"spam"if it contains at least 2 occurrences of words that appear inspamWords. - A text is
"not_spam"if it contains fewer than 2 such occurrences. - Matching is case-insensitive.
- Words are separated by single spaces (treat each token as a word).
- Each occurrence counts, including repeated spam words in the same text (for example,
"paid paid"counts as 2). - Only consider words that exactly match a spam word (no partial matches).
Return an array of length n, where the ith value is the label for texts[i].
Function
classifyTexts(texts: String[], spamWords: String[]) → String[]Examples
Example 1
texts = ["free prize worth millions", "ten tips for a carefree lifestyle"]spamWords = ["free", "money", "win", "millions"]return = ["spam", "not_spam"]The spam words found for each text are:
| Text | Spam Words Found | Answer |
|---|---|---|
free prize worth millions | free, millions | spam |
ten tips for a carefree lifestyle | - | not_spam |
Return ["spam", "not_spam"].