Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!