Skip to main content

Command Palette

Search for a command to run...

344. Reverse String LeetCode Solution

Data Structures and Algorithms | Problem Solving

Updated
2 min read
344. Reverse String LeetCode Solution
A

I love to help beginners in their programming journey with my writing and problem-solving skills.

You might have reversed the integer while doing a Palindrome Problem. But here, you asked to Reverse the string. This question is based on the basic String concepts. Let's see what the problem is and how we can write code as a solution to this.

Problem Statement

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Examples

Here are some sample examples through which you can understand the problem clearly.

Example 1:

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"] 

Example 2:

Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

Constraints

Solution

So, we looked into all the problem details and constraints. Now let's dive into solutions for the same. The entire logic for reversing a string is based on using the opposite directional two-pointer approach!

  1. We maintain two pointers. start and end, start point to first string character and end to the last character.
  2. We swapped those two characters. I.e. the first character will go to last and the last character will come to the first position.
  3. After doing this, we will increment the start pointer by 1 and decrement the end by 1.
  4. We will keep doing this until start is less than equal to the end pointer.
  5. This approach is often called as Two Pointer Approach
// C++ Solution

class Solution {
public:
    void reverseString(vector<char>& s) {

        // Initialise the two pointers
        int start = 0;
        int end = s.size()-1;

        while(start <= end){

            // Swap the characters; just like numbers
            char temp = s[start];
            s[start] = s[end];
            s[end] = temp;

            //update both the pointers
            start++;
            end--;
        }

    }
};

Conclusion

So, this is the solution to the Reverse String Leetcode Problem. Hopefully, you might understand this solution. If you find it useful, please follow me for more such articles. You can also leave your suggestions, thoughts in the comment box.

Thank you!

LeetCode Solutions

Part 8 of 11

In this series, you will get the solutions to LeetCode problems. Here I will be discussing best approach to solve the problem along with C++ Code.

Up next

704. Binary Search LeetCode Solution

Data Structures and Algorithms | Problem Solving