1009. Sort Characters in a String

Given a string s, write a program to sort only the alphabetic characters in the string in lexicographical (alphabetical) order.

  • Ignore numbers, whitespaces, and punctuation.
  • Output should contain only the sorted alphabetic characters (i.e., no digits or symbols retained).

Example 1:

Input: s = "zxcbg"

Output: "bcgxz"

Explanation: Sorted lowercase characters only.

Example 2:

Input: s = "TUF123#rocks!"

Output: "FORckstu"

Explanation: Letters sorted: F, O, R, c, k, s, t, u (case-sensitive sort).

Now Your Turn!

Pick the correct output for the given input

Input: s = "LeetCode2025!!"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 105
  • s contains letters (uppercase or lowercase), digits, punctuation, and spaces
  • Output must contain only sorted alphabet characters (both uppercase and lowercase preserved and included)
0
class Solution {
public:
string sortCharacters(const string& s) {
// Your code goes here
}
};
 
Test Case

Input:

S