Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Remove All Occurrence of A Character

Easy Acceptance 75% Points 20

You are given a string s and a character ch. Your task is to remove all occurrences of the given character from the string. Loops or built-in replace functions are not allowed. Return the final string after removal.

Examples
Example 1

Example 1

Input: s = "banana", ch = 'a'

Output: "bnn"

Explanation: All occurrences of 'a' are removed from the string.

Example 2

Example 2

Input: s = "hello", ch = 'l'

Output: "heo"

Explanation: Both 'l' characters are removed.

Example 3

Example 3

Input: s = "abc", ch = 'x'

Output: "abc"

Explanation: The character is not present, so the string remains unchanged.

Constraints
  • 1 = length of s = 105
  • The string contains printable characters only
Topics
Recursion
Companies
Apple Wipro Media.net
Added 19 Jul 2026 75% acceptance
Ln 1, Col 1Sign in to save your code
banana
a
bnn