KatInterview

Text Substitution with Whole-Word Match and Longest-Prefix Priority

StringsAuto-graded · 23 test cases
MediumAuto-gradedStringstriegreedystring-matching

Last updated

Apply a phrase-to-replacement map on the original text in one pass, respecting whole-word boundaries and preferring longer matches.

You are given a string document and two equal-length arrays find and replace_with. Each find[i] should be substituted with replace_with[i] wherever it appears in the document.

Your task is to return the transformed document after applying all substitutions simultaneously—that is, every match is determined against the original text, so replacements already inserted cannot themselves be matched or altered by later rules.

Rules

  1. Case-sensitive literal matching. Match each phrase exactly, including any internal spaces.
  2. Whole-word boundaries. A valid match must not be immediately preceded or followed by a word character. Word characters are ASCII letters (a-z, A-Z), digits (0-9), and underscore (_). All other characters—including spaces, punctuation, and the start or end of the string—are non-word.
  3. Longest match wins. When scanning left to right, if multiple phrases can match starting at the current position (and all satisfy the boundary requirement), choose the one that spans the most characters. Advance past the entire matched span and continue scanning from there.
  4. One-pass processing. Each position in the original document is examined exactly once. Text inserted by a replacement is never re-scanned.
  5. Unmatched characters are copied verbatim.

Constraints

  • 0 ≤ len(document) ≤ 10,000
  • 0 ≤ len(find) ≤ 100
  • len(find) == len(replace_with)
  • Each phrase in find is non-empty, begins and ends with a word character, and is unique.
  • Each string in replace_with may be empty (deletes the phrase) or arbitrarily long.
  • Each phrase and replacement has at most 100 characters.
  • All input is ASCII.
  • Distinct phrases of equal length will never match the exact same span simultaneously.

Examples

Example 1

Input: apply_substitutions('Person One emailed Person.', ['Person', 'Person One'], ['word', 'name'])

Output: 'name emailed word.'

Example 2

Input: apply_substitutions('red redwood blue', ['red', 'blue'], ['blue', ''])

Output: 'blue redwood '

Example 3

Input: apply_substitutions('alpha beta alpha', ['alpha', 'alpha beta'], ['X', 'Y'])

Output: 'Y X'

Hints

Hint 1

Sequential string replacements cascade: text inserted by one rule can be matched by another. Keep the scan index on the original text and write output to a separate buffer.

Hint 2

Build a trie of all phrases and walk it from each candidate start position. As you walk, record every phrase endpoint that also satisfies the right boundary condition; the last one recorded is the longest.

Hint 3

A phrase like 'cat' inside 'category' should not match if 'y' immediately follows. Check that the character after the match (if any) is not a word character before accepting the match.