You are given an array of messages messages and an array of unsafe words unsafeWords. Each message is an array of exactly two strings [username, text]. A message is unsafe if its text contains at least one unsafe word. Return only the safe messages, meaning those whose text contains no unsafe word, preserving the original order.
Word matching follows two rules:
- A word is a maximal sequence of characters delimited by spaces: punctuation is part of the word, not a separator, so
"Nintendo,"and"Nintendo"are distinct words. - Matching is case-insensitive:
"nintendo"and"NINTENDO"are the same word.
Constraints:
- 0 ≤
messages.size()≤ 104 - 0 ≤
unsafeWords.size()≤ 104 usernameandtextare non-empty strings.textcontains only printable ASCII characters.- Words in
textare separated by single spaces; there are no leading or trailing spaces. - Each entry in
unsafeWordsis a non-empty string that contains no spaces.
Example 1:
Input: messages = [["tom", "i am enjoying the nintendo switch"], ["jerry", "i prefer the wii"], ["tom", "my best friend is sonya"], ["tom", "do you have a usb charger"], ["jerry", "no but i do have a USB-C charger"], ["dan", "dafNintendo-doo is a fun app"]], unsafeWords = ["Nintendo", "Microsoft", "SONY", "usb-C"] Output: [["jerry", "i prefer the wii"], ["tom", "my best friend is sonya"], ["tom", "do you have a usb charger"], ["dan", "dafNintendo-doo is a fun app"]]
Explanation: Each message is checked by comparing its space-delimited tokens against the unsafe words.
- tom ("i am enjoying the nintendo switch"): the token
"nintendo"matches"Nintendo"case-insensitively, so this message is unsafe and excluded.- jerry ("i prefer the wii"): no token matches any unsafe word, so this message is safe.
- tom ("my best friend is sonya"):
"sonya"≠"sony"(different tokens), so this message is safe.- tom ("do you have a usb charger"):
"usb"≠"usb-C"(punctuation makes them distinct), so this message is safe.- jerry ("no but i do have a USB-C charger"):
"USB-C"matches"usb-C"case-insensitively, so this message is unsafe and excluded.- dan ("dafNintendo-doo is a fun app"):
"dafNintendo-doo"≠"Nintendo"(different token), so this message is safe.
Example 2:
Input: messages = [["a", "Nintendo is great"], ["b", "I love my NintendoSwitch"], ["c", "SONY sells TVs"], ["d", "sonya is my friend"], ["e", "Buy usb-c today"], ["f", "I use USB cable"]], unsafeWords = ["Nintendo", "SONY", "usb-C"] Output: [["b", "I love my NintendoSwitch"], ["d", "sonya is my friend"], ["f", "I use USB cable"]]
Example 3:
Input: messages = [["a", "Nintendo, is mentioned"], ["b", "Nintendo is mentioned"], ["c", "I like usb-c."], ["d", "I like usb-c"]], unsafeWords = ["Nintendo", "usb-c"] Output: [["a", "Nintendo, is mentioned"], ["c", "I like usb-c."]]
