Skip to content
CodeKruPro

Sort the characters of a string

Coding problemEasyAsked in: TCS

Given a string s, return its characters sorted by character code, smallest first.

A character code is the number a computer stores for a character, such as 65 for A and 97 for a. Character code order puts the space first, then punctuation and digits, then upper-case letters, then lower-case letters (with some punctuation in between). So "BaA" becomes "ABa". Repeated characters stay repeated.

Examples

Example 1
Input: s = "banana"
Output: "aaabnn"
Example 2
Input: s = "BaA"
Output: "ABa"
Upper-case letters have smaller codes than lower-case letters.
Example 3
Input: s = "a1 B"
Output: " 1Ba"

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.

to answer and keep your progress. Reading is free for everyone.