Leetcode 344: Reverse String
You are given a list of characters s. Your task is to reverse the order of characters in the list in-place, modifying the input list directly and without using any extra memory.
Problem
Approach
Steps
Complexity
Input: The input is a list of characters.
Example: s = ["a", "b", "c", "d"]
Constraints:
• 1 <= s.length <= 10^5
• Each character in the list is a printable ASCII character.
Output: The output is the input list with the characters reversed in place.
Example: ["d", "c", "b", "a"]
Constraints:
• The input list must be modified in-place.
Goal: The goal is to reverse the list of characters in place, without using extra memory.
Steps:
• Initialize two pointers, one at the start and the other at the end of the list.
• Swap the characters at these two pointers and move the pointers towards each other.
• Continue swapping until the pointers meet or cross.
Goal: The input list must have a length between 1 and 100,000.
Steps:
• The solution should be optimized for large inputs.
Assumptions:
• The input is a valid list of printable ASCII characters.
• Input: s = ["a","b","c","d"]
• Explanation: The list is reversed in place, resulting in ["d","c","b","a"].
• Input: s = ["r","e","v","e","r","s","e"]
• Explanation: The list is reversed in place, resulting in ["e","s","r","e","v","e","r"].
Approach: The solution involves reversing the input list in place by swapping elements from both ends towards the center.
Observations:
• The problem requires modifying the list directly, using two pointers to swap elements in place.
• The approach should use constant space and iterate through the list in a linear fashion.
Steps:
• Initialize two pointers, i = 0 and j = s.size() - 1.
• While i < j, swap the characters at s[i] and s[j], then increment i and decrement j.
• Repeat until the pointers meet or cross, ensuring the list is reversed in place.
Empty Inputs:
• The input list should not be empty as the length is guaranteed to be >= 1.
Large Inputs:
• The solution must be efficient enough to handle input sizes up to 100,000.
Special Values:
• When the list has only one character, the list remains unchanged after reversal.
Constraints:
• The solution must operate in O(n) time complexity and O(1) space complexity.
void reverseString(vector<char>& s) {
int i = 0, j = s.size() - 1;
while(i <= j) {
swap(s[i++], s[j--]);
}
}
1 : Function Declaration
void reverseString(vector<char>& s) {
This is the function declaration for `reverseString`. The function takes a reference to a vector of characters, `s`, and modifies it to reverse the characters in place.
2 : Variable Initialization
int i = 0, j = s.size() - 1;
Initialize two pointers: `i` starts at the beginning of the vector, and `j` starts at the end of the vector. These pointers will be used to swap characters from both ends.
3 : Loop Until Pointers Meet
while(i <= j) {
The while loop continues as long as `i` is less than or equal to `j`. This ensures that we are swapping characters until the pointers meet or cross.
4 : Swap Characters
swap(s[i++], s[j--]);
Swap the characters at positions `i` and `j`. The `i++` and `j--` increment and decrement the pointers after each swap to move toward the center of the vector.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is linear since the solution requires a single pass over the list.
Best Case: O(1)
Worst Case: O(1)
Description: The space complexity is constant since the solution only uses two pointers.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus