344. Reverse String LeetCode Solution
Data Structures and Algorithms | Problem Solving
Table of contents
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 link: Reverse String
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
- 1 <= s.length <= 105
s[i]
is a printable ascii character.
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!
- We maintain two pointers.
start
andend
, start point to first string character and end to the last character. - We swapped those two characters. I.e. the first character will go to last and the last character will come to the first position.
- After doing this, we will increment the
start
pointer by 1 and decrement theend
by 1. - We will keep doing this until
start
is less than equal to theend
pointer. - 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!