Text rewrite rules
A documentation build pushes every source line through a chain of text rewrite rules. Each entry of rules is written "find|replace" and splits on its FIRST bar only, so the find text never holds a bar but the replacement may. Run the rules over a line in the order given, each rule finishing the whole line before the next one starts, so a later rule works on what the earlier ones produced. Inside one rule, scan left to right and replace every occurrence, resuming after the text just inserted, so a rule never rewrites its own output. Matching is case sensitive. Skip any rule whose find text is empty, since it would match everywhere. Return the rewritten lines in the order they were given.
apply_rewrite_chain(lines: list[str], rules: list[str]) → list[str][["ship TODO by friday"],["TODO|FIXME"]]out["ship FIXME by friday"][["alpha"],["alpha|beta","beta|gamma"]]out["gamma"][["aaaa"],["aa|a"]]out["aa"]State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
[["ship TODO by friday"],["TODO|FIXME"]]["ship FIXME by friday"]not run yetsample[["alpha"],["alpha|beta","beta|gamma"]]["gamma"]not run yetsample[["aaaa"],["aa|a"]]["aa"]not run yetsample