← Questions

Detect disguised abusive words in comments

0
AmazonCoding

A comment platform wants to catch abusive words that users disguise with look-alike characters, such as $ for s or 0 for o.

Implement AbuseDetector:

  • AbuseDetector(banned, substitutions): banned is a list of lowercase words. substitutions maps a disguise character to the letters it can stand for, as a string. One character can stand for several letters, for example "1": "il".
  • detect(comment) -> List[str]: returns the banned words found in the comment, in order of appearance.
  • flag(comments) -> List[int]: returns the indices of comments that contain at least one banned word.

Rules

  • Matching is case-insensitive.
  • A token is a maximal run of characters that are letters, digits, or disguise characters. Every other character separates tokens.
  • A token matches a banned word when the whole token can spell it, reading each character either as itself (if it is a letter) or as one of the letters it can stand for. A banned word inside a longer token does not count.

Constraints

  • Up to 10^5 banned words with total length up to 10^6.
  • Comments up to 10^4 characters. A character can stand for up to 3 letters.

Example

banned = ["jerk", "idiot", "loser"]
substitutions = {"$": "s", "0": "o", "3": "e", "!": "i", "1": "il", "|": "il"}

detect("What an !d!0t, honestly. J3RK.")  ->  ["idiot", "jerk"]
detect("|0$3r")                            ->  ["loser"]
detect("jerky snacks")                     ->  []
flag(["great post", "j3rk", "l0ser!"])     ->  [1]

In the last call, ! is a disguise character, so l0ser! is one token. It spells loseri, which is not banned.

Part 2. Users also stretch words, as in jeeeerk or l0000ser. Add an option where each letter of a banned word may appear one or more times in a row. Interviewers may then ask about separators inside words (j.e.r.k), banned words inside longer tokens, and multi-character disguises such as |< for k.

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding