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

119
Visualizações
Find out longest palindromic substring

I want to run this code and I am getting aba but not bab. How can I get the solution from this code? Suggestions with explanations so that I can understand properly.

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
var longestPalindrome = function(s) {
    let splitString = s.split("");
    let reverse = splitString.reverse()
    console.log(reverse)
    let check_value = s.split("")
    console.log(check_value)
    let store = []
    check_value.map((x,id) => {
        if(x == reverse[id]){
            store.push(x)
        }
    })
    console.log(store)                               
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your function is not doing the job right. It assumes that the center of the palindrome is always in the center of the input string. So that explains why "bab" is not found.

But even worse, it will even give false positives.

For instance:

Input: "dobad"
Output: ["d", "b", "d"]

So you'll need to revisit the code challenge. The approach you had in mind is not the right one.

If you cannot find how to do it, have a look at Longest palindromic substring on Wikipedia. Also on this site there are several Q&A on the subject.

about 4 years ago · Juan Pablo Isaza Relatório

0

So, you have these letters in the arrays: check_value => 0: b, 1: a, 2: b, 3: a, 4: d reverse => 0: d, 1: a, 2: b, 3: a, 4: b

And in your algorithm, you are doing

First step:

  • x = b
  • id = 0
  • reverse[id] = d x != d then the value is not saved

Second step:

  • x = a
  • id = 1
  • reverse[id] = a x == a then the value is saved
  • store = [a]

And so on, so that is the reason why the algorithm is not working

A tip, iterate the string and find the next index of the current character, so, if the character is b find the indexes of any other b in the same array and validate if the substring generated between the current character index and the found indexes is a palindrome

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