Dima Korolev
1 min readMar 19, 2016

--

First and third methods are extremely slow comparing to the second one. And still the second method is not that fast as it could be — it has only Θ(n) complexity.

When you have a string, you already know it’s length. Thus, you don’t need to iterate it from the begining to the end, you can just literally rotate it around the middle:

This method has a complexity almost Θ(n/2) and runs 1.5–2 times faster, than the second method described in the article.

Just benchmark it.

--

--