call(back)
Algorithms & Data StructuresmediumSign in to star problems

Review Token Tagging

Tag the tokens in a guest review. Given the review text and a map of token → label, wrap each case-insensitive occurrence of a token as [label]{original text}, keeping the review's original casing inside the braces.

tagTokens("Great WiFi here", { wifi: "amenity" })   ->  "Great [amenity]{WiFi} here"

Rules

  • Multi-word tokens ("hot tub") match across the space.
  • When several tokens match at the same position, the longest wins: with pool and pool view both defined, "pool view" is tagged once, as the longer token.
  • Every occurrence is tagged; text that matches nothing is passed through unchanged. An empty token map returns the review untouched.

Hints

Hint 1

Build a trie over the lowercased tokens and store the label on each token's final node. Then walk from every offset of the lowercased review.

Hint 2

Don't stop at the first terminal node — keep walking while the trie still has a path and remember the last terminal you passed. That's what makes the longest match win.

Hint 3

Slice the original review for the braces, advance past the match, and fall through one character at a time when nothing matches.

Asked at