Home | Projects | Notes > Problem Solving > LC - E - 344. Reverse String

LC - E - 344. Reverse String

 

Solutions in C++

Solution 1

Even if s forms a string it is essentially a vector. The container vector does not provide the function length(). Make sure to use size().

String class also provides the size() member function. (Maybe a good idea to use size() all throughout to minimize confusion.)

Complexity Analysis:

Solution:

 

Solution 2

This solution uses beg and end index variables.

Complexity Analysis:

Solution:

 

Solution 3

This solution uses C++ STL stack container to reverse a string. Note that this solution does not satisfy the condition "you must do this by modifying the input array in-place with O(1) extra memory".

Complexity Analysis:

Solution:

 

Solution 4

This solution uses the C++ standard library function reverse() to reverse a string.

Complexity Analysis:

Solution: