Task
You will receive a string consisting of lowercase letters, uppercase letters, and digits as input. Your task is to return this string as blocks separated by dashes ("-"). The elements of a block should be sorted with respect to the hierarchy listed below, and each block cannot contain multiple instances of the same character.
The hierarchy is:
- lowercase letters (a - z), in alphabetic order
- uppercase letters (A - Z), in alphabetic order
- digits (0 - 9), in ascending order
Examples
"21AxBz" -> "xzAB12"
- since input does not contain repeating characters, you only need 1 block"abacad" -> "abcd-a-a"
- character "a" repeats 3 times, thus 3 blocks are needed"" -> ""
- an empty input should result in an empty output
This challenge comes from airatmulk on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (4)
Solution in Haskell.
It sorts the input string, then groups it into arrays of each piece (e.g. "abbcddde" -> ["a", "bb", "c", "ddd", "e"]. The
blocks
method combines corresponding elements of the sub-arrays after grouping (continuing the example -> ["abcde", "bd", "d"]). Then the strings are joined by a hyphen. My least favorite part of my solution is thecompareLetters
function which determines the order of 2 letters for letters. I couldn't come up with a better way to do this.in python
python
Someone ask on SO, and I answered:
answer re: Return String as Sorted Blocks - Codewars Challenge - JavaScript
For funzies, here's how I would have approached the problem: