#1598Easy· asked at 1 company
Remove Characters Except Alphabets
Problem
Given a string s, return a new string containing only the alphabetic characters of s (a–z, A–Z), in their original order. Digits, spaces, punctuation and other symbols are dropped.
Input / Output
- Input: string
s. - Output: filtered string.
Examples
Example 1
Input: s = "He11o, W0rld!"
Output: "HeoWrld"
Example 2
Input: s = "123 456"
Output: ""
Example 3
Input: s = "abc"
Output: "abc"
Constraints
0 <= s.length <= 10^5
Approach
Single pass; keep a character if it is a letter. Two ways to test:
isalpha()/Character.isLetter— Unicode-aware, accepts accented letters.- Range check
'a' <= c <= 'z' or 'A' <= c <= 'Z'— ASCII only, what most such problems intend.
Use a builder/list and join once; repeated string concatenation is quadratic in immutable-string languages.
This is the filtering half of "Valid Palindrome" (LeetCode 125), where you strip non-alphanumerics and lowercase before comparing — knowing this problem makes that one a two-liner.
Complexity
- Time: O(n)
- Space: O(n) for the output.
Reference solution (Python)
def only_alphabets(s: str) -> str:
return ''.join(c for c in s if ('a' <= c <= 'z') or ('A' <= c <= 'Z'))
Test cases
| Input | Expected |
|---|---|
"He11o, W0rld!" | "HeoWrld" |
"123 456" | "" |
"abc" | "abc" |
"a-b_c" | "abc" |