Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
Find a secret word given an array of triplets? (codewars problem)

My code works and I need help figuring out how to optimize it. If possible, don't give me any code, just tips on optimizing it, please.

The rules for the puzzle are:

  1. Each triplet has the rules of how the letters are ordered in the secret word (each letter is followed by the next letter inside the triplet array).
  2. all the letters of the secret word are distinct.

Example input:

triplets1 = [
  ['t','u','p'],
  ['w','h','i'],
  ['t','s','u'],
  ['a','t','s'],
  ['h','a','p'],
  ['t','i','s'],
  ['w','h','s']
]

Expected Output: "whatisup"

I came up with this code, which works. It selects the first letters that are not preceded by any others in the arrays they are placed, removes them, concatenates them into the final word, and keep doing that until all the arrays are empty. The code isn't being accepted though, because is exceeding the time limit.

function recoverSecret(triplets) {
    let secretWord = '', character = '';
    let notEmpty = true;
    let size = triplets.length;

    //it loops until array is empty
    while(notEmpty) {
        notEmpty = false;
        for (let i = 0; i < size; i++) {
            for (let j = 0; j < triplets[i].length; j++) {
                    if (character) j = 0; //everytime a character is included, this condition is truthy, so you have to go back to the start of the array because the character was removed last iteration
                    character = triplets[i][j];
                    let remove = []; //this array will have the positions of the letter to remove in the removal cycle
                    for (let k = 0; k < size; k++) {
                
                        if (character == triplets[k][0]) remove.push(k);
                        //if the letter is in the triplet and it's not the first position, then it isn't the letter we're looking for, so character equals to '', otherwise it will be the letter which will be added to the secretWord string
                        if (k != i && (triplets[k].includes(character))) {
                            if (character != triplets[k][0]) {
                                character = '';
                                break;
                            }
                        }
                    }
                    secretWord += character;
                    if (character) {
                        //if character is not '', then a removal loop is done to remove the letter because we just found its place
                        for (x of remove) {
                            triplets[x].shift();
                        }
                    }
                // if (triplets[i].length == 0) break;
            }
                if (triplets[i] != 0) notEmpty = true; //if every triplet is empty, notEmpty remains false and while loop is over
        }
    }
    return secretWord;
}

If possible I don't want any code, just tips on optimization, please.

about 4 years ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda