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].
Examples
01 · 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"].
More IBM problems
- Maximum Requests in a Time WindowOA · Seen Jul 2026
- Query Type Frequency WindowOA · Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA · Seen Jul 2026
- Parent Process NumberOA · Seen Jun 2026
- Request Retry CountOA · Seen Jun 2026
- Count Ideal NumbersOA · Seen Jun 2026
- Count Descending SubarraysOA · Seen Apr 2026
- Count Power Products in RangeOA · Seen Apr 2026
public String[] classifyTexts(String[] texts, String[] spamWords) {
// write your code here
}texts["free prize worth millions", "ten tips for a carefree lifestyle"]
spamWords["free", "money", "win", "millions"]
expected["spam", "not_spam"]
checking account