LeetCode #344 — Reverse String

David Seek
2 min readJun 4, 2020

Find more useful articles at www.davidseek.com

LeetCode’s challenge of June 4, 2020 (#344) asks us to reverse a String. This task can be achieved with 1 line of code, yet it’s worth exploring the different options. Interviewers will want you to show that you can explain different approaches.

Two Pointer Approach

Interviewers very often ask about two pointer approaches, and it’s very likely that you may be asked not to rely on built in functions, which make this solution viable.

The advantage of this approach is that the work is logically cut in half. You have a pointer on the left and a pointer on the right. You then swap the characters and you move the pointers along until they cross and the work is done.

Remember to always have an exit condition when doing work recursively or you might end up with a stack overflow (endless loop).

This solution has a runtime of O(n / 2) and is expressed as O(n).

Remember to always drop constants from Big O notations.

--

--