Problem 482867 · medium · Level 04 Non-Linear Data Structures

Redact One Letter

recursion · strings · filtering

A document scrubber must remove every copy of one specific character from a line of text. Write redact(s, ch) returning s with all occurrences of the single character ch deleted, using recursion (no loops, no str.replace, no comprehensions).

Examples

Input:  s = "balloon", ch = "l"
Output: "baoon"

Input:  s = "aaa", ch = "a"
Output: ""

Constraints

  • 0 <= len(s) <= 2000; ch is one character.
  • Recursion depth is at most len(s).

Goals

  • Decide per character whether to keep it, then recurse on the rest
  • Return an empty string from the base case
  • Understand that string concatenation drives the cost of naive recursion
Starting Python…