Sobes.tech
Intern — Senior

String encoding using RLE method

livecode

Task condition

Given a string consisting of uppercase Latin letters A-Z, for example: "AAAABBBCCXYZDDDEEFFFFAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBB". It is required to implement a function rle that transforms it into a form where consecutive identical characters are replaced by the character and the number of its repetitions. The result for the given example should look like this: "A4B3C2XYZDAE3F3 328".

Important:

  1. If a character appears only once, it remains unchanged.
  2. If a character repeats more than once, immediately add the number of its repetitions.
 function rle(str) {
    // your code here
}

rle('A') // A
rle('AAAAB') // A3B
rle('ABCCC') // ABC3

Write an implementation considering the above rules.