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

115
Visualizações
Longest Common Substring (more than 2 arguments)

I have seen the solution for LCS 2 strings. Below is the code. I am curious how can I change it so that it can solve properly when more than 2 strings are given.

I would appreciate any help or resource that can be useful Thank you.

const printLCS = (a, b) => {
    let m = a.length;
    let n = b.length;
    let lcs = new Array(m + 1);
    let lcsLen = 0;
    let row = 0, col = 0;

    for (let i = 0; i <= m; i++) {
        lcs[i] = Array(n + 1);
        for (let j = 0; j <= n; j++) {
            lcs[i][j] = 0;
            if (i == 0 || j == 0) {
                lcs[i][j] = 0;
            } else if (a[i - 1] == b[j - 1]) {
                lcs[i][j] = lcs[i - 1][j - 1] + 1;
                if (lcsLen < lcs[i][j]) {
                    lcsLen = lcs[i][j];
                    row = i;
                    col = j;
                }
            } else {
                lcs[i][j] = 0;
            }
        }
    }

    if (lcsLen == 0) {
        console.log("No Common Substring");
        return;
    }

    let resStr = "";

    while (lcs[row][col] != 0) {
        resStr = a[row - 1] + resStr;
        --lcsLen;
        row--;
        col--;
    }
    console.log(resStr);
}

const myArgs = process.argv.slice(2);
printLCS(myArgs[0], myArgs[1]);

const onErr = (err) => {
    console.log(err);
    return 1;
}

about 4 years ago · Santiago Gelvez
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