Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
run length encoding for a string in JavaScript

I'm a new developer and got some questions from a test this week and coundn't find a way to solve this one. Any ideas?

The problem I believe is to sort a Javascript string which you have to convert the letters in a row to a new string which adds the times the letter is reapeated to the string; example:

For the string = 'AAABBBBCCX9999!!!!!!**' it should return string = '3A4B2CX496!2*'

I tried some strings and arrays methods but didn't come close. I'm not in a hurry since the test is gone. But would be fun see it resolved; thanks everyone!

I'm still learning so thank you all for your answers and feedback about how to use stack overflow.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It works by matching on the string for all sequences of consecutive characters, giving you an array of each group of consecutive characters, which are then each mapped to a new string of the group's length and the first character put together (AAA becomes 3A ('AAA'.length + 'AAA'[0])). Then each string is glued back together again with .join('').

const seq = 'AAABBBBCCX9999!!!!!!**'
  .match(/(.)\1*/g).map(s => s.length + s[0]).join('');
  
console.log(seq);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda