Skip to content
CodeKruPro

Remove special characters, keep letters and digits

Coding problemEasyAsked in: Infosys

Given a string s, remove every character that is not an English letter (a to z, A to Z) or a digit (0 to 9). Return what is left, in the original order.

Spaces and punctuation are removed too. Interviewers often ask for this without regular expressions (short patterns that match text), so walk the characters yourself.

Examples

Example 1
Input: s = "Hello, World! 123"
Output: "HelloWorld123"
Example 2
Input: s = "user.name+qa@test.com"
Output: "usernameqatestcom"
Example 3
Input: s = "!!!"
Output: ""

Constraints

  • The length of s is between 0 and 100,000
  • s holds printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in