Remove the spaces and sort
MediumStrings10 min
Write a function sortCharacters(text) that removes all whitespace from the string text and returns the other characters sorted.
texthas only ASCII characters: letters, digits, punctuation and whitespace.- Whitespace is a space, a tab
\tand a line break\nor\r. All of it goes, not only spaces. - Each character is sorted by its character code, from small to large. This is the order of sort without a compare function. For example,
!and,come before the digits, the digits come before the capital letters, and the capital letters come before the small letters. So'Z'comes before'a'. - Repeated characters stay:
'banana'gives'aaabnn'. - Return a string. An empty string or a string of only whitespace gives
''.
Examples
sortCharacters('My Name is Nikhil')Returns 'MNNaehiiiklmsy'
sortCharacters('Zebra 123')Returns '123Zaber'
Asked at Cognizant · Source
Loading the editor...
- Input
sortCharacters('My Name is Nikhil')Expected'MNNaehiiiklmsy' - Input
sortCharacters('b a')Expected'ab'
Hidden tests run on Submit