<https://codeshare.io/2EbP7O>
# announcements
w
a
which execution time limit?
w
hey its a competitive coding practice question from leetcode...
they have an execution time limit in place for their large data sets
to make sure the code is fast enough
a
well the only thing you can do is compare the string directly inside
isPalindrome
instead of "converting" it into a
CharArray
, but there is not much more performance to get in the way you do it
w
makes sense. the 'conversion' is of order N I am assuming?
a
what do you mean with order N? O(N)?
w
yes O(N)
gotcha. so I have apparently increased the worst case complexity from O(N^2) to O(N^3)...
thanks a lot!
a
yes,
toCharArray()
copies every character one by one and because you do it twice + you reverse it, you are iterating over the string 3 times
+ the comparsion iterates too over the whole array
p
Normally when you run into a timeout on challenges like that, it's because you need a different algorithm with a better runtime. For this problem, there's a DP approach: https://www.geeksforgeeks.org/longest-palindrome-substring-set-1/
w
@Andreas Sinz Got it.
@petersommerhoff Yup but in this case somehow I couldn't think of a faster solution...clearly I was wrong!
thanks a lot for the link I'm studying it right now 🙂
p
@warriorprincess Didn't say it's easy, it's actually the hardest part simple smile
w
@petersommerhoff yup DP problems are usually difficult for me
p
Not just for you but that just means it's good to practice
👍🏼 1