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

183
Views
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 answers
Answer question

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 Report

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 Report
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!