Strip Unwanted Characters from Input

Medium ⏱ 12 min 55% acceptance ★★★★☆ 4.4
Write remove_chars(s, chars_to_remove) that returns s with every character that appears in chars_to_remove deleted, leaving all other characters (including spaces) untouched.

Examples

Example 1
Input
s = 'Hello, World!123', chars_to_remove = ',!123'
Output
'Hello World'
Explanation

Every character found in ',!123' is removed; letters and the space survive.

Constraints

  • 0 <= len(s) <= 10^4

Topics

StringsSanitization

Companies

PayPalRazorpay

Hints

Hint 1

Convert chars_to_remove into a set for O(1) membership checks.

Hint 2

Build the result with a generator expression that keeps characters not in that set.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.