Skip to the content

Sort the letters

Problem

A word-guessing game gives a hint before each round: it shows the letters of the secret word lined up from a to z, so you see all of them without giving the order away. For "planet" the hint is "aelnpt".

Write a function that takes a word and returns a new string with its letters sorted from a to z.

The rules: capitals don't change where a letter goes and the hint comes out all in lowercase, so for "Sofia" the hint is "afios". Repeated letters all stay, none gets lost: for "banana" the hint is "aaabnn". If an empty string arrives, you return an empty string. The words arrive in one piece: no spaces, no accents, plain letters only.

Watch what you return: it is a string, not a list of letters.

Examples

  • With a repeated letter

    "onion" → "innoo"

  • Already in order

    "almost" → "almost"

  • Starts with a capital

    "Nora" → "anor"

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def sort_letters(word):
    pass

JavaScript

function sortLetters(word) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →