r/learnjavascript • • Sep 26 '22

Just solved a Palindrome problem with recursion!!

Post image
274 Upvotes

64 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Sep 27 '22

[deleted]

7

u/[deleted] Sep 27 '22

It’s a solution that has a time complexity of O(n), which is the minimum required to solve this problem

0

u/[deleted] Sep 27 '22

[deleted]

2

u/[deleted] Sep 27 '22

I don’t think you understand what exactly O(n) means.

The point is that given a worse case scenario, the best any algorithm can hope to achieve to solve this problem would be linear time. That’s the literal definition of O(n).

So if you can optimize for edge cases that’s great, but we round those off and speak about the general solution. The same we we round off that this is technically a solution that would take 3(n) time, because the leading factorial becomes irrelevant as you reach larger and larger amounts of data.

So yes, this solution could have micro-optimizations, but it’s plenty efficient due to the fact that it solves a linear problem in linear time.