Skip to content

Line Encoding

Given a string, return its encoding defined as follows:

  • First, the string is divided into the least possible number of disjoint consisting of identical characters

    • for example, "aabbbc" is divided into ["aa", "bbb", "c"]
  • Next, each substring with length greater than one is replaced with a concatenation of its length and the repeating character

    • for example, substring "bbb" is replaced by "3b"
  • Finally, all the new strings are concatenated together in the same order and a new string is returned.

Example

For s = "aabbbc", the output should be

line_encoding(s) = "2a3bc"

Input/Output

  • [input] string s

    String consisting of lowercase English letters.

Solution

py
import itertools


def line_encoding(s):

    result = ''
    for label, group in itertools.groupby(s):
        count = sum(1 for _ in group)
        if count > 1:
            result += '{}{}'.format(count, label)
        else:
            result += label

    return result


print(line_encoding('ccccccccccccccc'))
print(line_encoding('abbccc'))
js
// read the python version of this snippet from this folder
// and convert it to javascript

function lineEncoding(s) {
  let count = 1;
  let result = '';
  for (let i = 0; i < s.length; i++) {
    if (s[i] == s[i + 1]) {
      count++;
    } else {
      if (count > 1) {
        result += count + s[i];
      } else {
        result += s[i];
      }
      count = 1;
    }
  }
  return result;
}

console.log(lineEncoding('aabbbc'));

my thoughts are neither my employer's nor my wife's