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

184
Visualizações
Time and Space complexity of this checking if string is palindrome

I want to verify my assumptions about Time and Space complexity of two different implementations of valid palindrome functions in JavaScript.

In the first implementation we are using a helper function and just pass pointers

const isPalindrome = str => {
  return isPalindromeHelper(str, 0, str.length - 1);
}

const isPalindromeHelper = (str, start, end) => {
  if (end - start < 1) return true;

  return str[start] === str[end] && isPalindromeHelper(str, start + 1, end - 1)
}

In this case I am assuming that the time complexity is O(N) and the space complexity is O(N) as well.

However, let's say that instead of passing pointers we are slicing the string each time. And let's say that slice is O(n) operation.

const isPalindrome = str => {
  if (str.length <= 1) return true;
  if (str[0] !== str[str.length - 1]) return false;
  return isPalindrome(str.slice(1, str.length - 1));
}

Would this push both Time and Space complexity to O(N^2) if slice was O(N) operation? Time because of time complexity of slice and Space would increase since we are constantly creating new strings?

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

0

Would this push both Time and Space complexity to O(N^2) if slice was O(N) operation? Time because of time complexity of slice...

Yes, if we assume slice has a time complexity of O(𝑛), then we have O(𝑛−1 + 𝑛−2 + 𝑛−3 + ... + 1) which is O(𝑛²)

...and Space would increase since we are constantly creating new strings?

Again, if we assume slice allocates new memory for the slice, then we have (with the same formula) a space usage of O(𝑛²)

However...

As strings are immutable, a JavaScript engine may benefit from memory that already has the string, and not really allocate new memory for slices. This is called string interning. This could bring both time and space complexity back to O(𝑛). However, since there is no requirement in the EcmaScript language specification to actually do this, we have no guarantee.

about 4 years ago · Juan Pablo Isaza Relatório

0

Both of them are recursive operations and run through the whole length of the string (n) n/2 times since they start at 0 and at n - 1 and they run until they meet at n/2.

In the O notation, this would mean both are O(n) since you can ignore the constant 2.

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