← all problems

runLengthEncode 🌶️🌶️🌶️ Extra

Run-length encoding compresses a string by replacing each run of consecutive identical characters with the count of that run followed by the character itself. Write a method that takes one String parameter, s, and returns its run-length encoding. For example, runLengthEncode("aaabbbcc") should return "3a3b2c" (three a's, then three b's, then two c's). You may assume s has at least one character. Runs of the same character that are NOT next to each other stay separate: runLengthEncode("aabbaa") returns "2a2b2a", not "4a2b".