Remove character

Easy
0/40
43 upvotes
Asked in companies
Expedia GroupThought WorksHewlett Packard Enterprise

Problem statement

For a given string(str) and a character X, write a function to remove all the occurrences of X from the given string and return it.

The input string will remain unchanged if the given character(X) doesn't exist in the input string.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a string without any leading and trailing spaces.

The second line of input contains a character(X) without any leading and trailing spaces.
Output Format:
The only line of output prints the updated string. 
Note:
You are not required to print anything explicitly. It has already been taken care of.
Constraints:
0 <= N <= 10^6
Where N is the length of the input string.

Time Limit: 1 second
Sample Input 1:
aabccbaa
a
Sample Output 1:
bccb
Sample Input 2:
xxyyzxx
y
Sample Output 2:
xxzxx
Approaches (1)
Iterative Solution
  • We create an empty string that will store our final result. We will return it at the end.
  • We iterate from 0 to the length of the string. Say, our loop variable is i. We can then access the ith character as input[i]
  • If the ith character equals the character c i.e input[i] == c, then we skip this character and do nothing.
  • Else we need to append the character to the output string.
  • We finally return the output string.
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Remove character
Full screen
Console