Pull to refresh

Comments 5

Спасибо за перевод. Если подумать, то первую задачку можно решить чуть короче:
function encodeString(s) {
    // "AAAABB" -> [["AAAA", "A"], ["BB", "B"]]
    const matches = s.matchAll(/(.)\1*/g)
    const mapFn = ([s, с]) => s.length + с
    return Array.from(matches, mapFn).join('')
}

function decodeString(s) {
    // "4A2B" -> [["4A", "4", "A"], ["2B", "2", "B"]]
    const matches = s.matchAll(/(\d+)(.)/g)
    const mapFn = ([, n, c]) => c.repeat(n)
    return Array.from(matches, mapFn).join('')
}
первая регулярка крутая, пойду почитаю
можно еще вспомнить про replace:
encodeString = str => str.replace(/(\w)\1*/g, (x, y) => x.length+y)

decodeString= str => str.replace(/(\d+)(\w)/g, (_,x, y) => y.repeat(x))
Отличное решение. Вы выиграли. :)
Sign up to leave a comment.

Articles