01
Write a function to reverse a string in place without using std::reverse.
Show answer
#include <string>
#include <utility>
void reverseInPlace(std::string& s) {
if (s.empty()) return;
std::size_t i = 0, j = s.size() - 1;
while (i < j) {
std::swap(s[i], s[j]);
++i;
--j;
}
}
// Two pointers meet in the middle: O(n) time, O(1) extra space.