← Home

344. Reverse String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
344

Let's make this one less mysterious. For 344. Reverse String, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • Two Pointer

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are reverseString.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The code keeps the moving pieces local, so the main idea stays visible.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Two Pointer
02//Runtime: 44 ms, faster than 92.88% of C++ online submissions for Reverse String.
03//Memory Usage: 23.5 MB, less than 6.10% of C++ online submissions for Reverse String.
04class Solution {
05public:
06    void reverseString(vector<char>& s) {
07        int i = 0, j = s.size()-1;
08        
09        while(i < j){
10            swap(s[i++], s[j--]);
11        }
12    }
13};
14
15//one variable
16//Runtime: 56 ms, faster than 53.92% of C++ online submissions for Reverse String.
17//Memory Usage: 23.3 MB, less than 6.10% of C++ online submissions for Reverse String.
18class Solution {
19public:
20    void reverseString(vector<char>& s) {
21        int i = 0, n = s.size();
22        
23        while(i < n/2){
24            swap(s[i], s[n-1-i]);
25            i++;
26        }
27    }
28};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.