KatInterview

344. Reverse String

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to reverse a string that is given as an array of characters. The key constraints are that we must modify the array in-place and use only O(1) extra memory. This means we cannot create a new array to store the reversed string; we must manipulate the given array directly with a constant amount of extra space.

Solution

Reverse String in Python

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        # Initialize two pointers: 'left' at the beginning and 'right' at the end of the list.
        left = 0
        right = len(s) - 1

        # Loop until the two pointers meet or cross each other.
        while left < right:
            # Pythonic way to swap elements in a list.
            s[left], s[right] = s[right], s[left]

            # Move the pointers towards the center of the list.
            left += 1
            right -= 1

Reverse String in Java

class Solution {
    public void reverseString(char[] s) {
        // Initialize two pointers: 'left' at the beginning and 'right' at the end of the array.
        int left = 0;
        int right = s.length - 1;

        // Loop until the two pointers meet or cross each other.
        while (left < right) {
            // Swap the characters at the left and right pointers.
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;

            // Move the pointers towards the center of the array.
            left++;
            right--;
        }
    }
}

Reverse String in C++

class Solution {
public:
    void reverseString(vector<char>& s) {
        // Initialize two pointers: 'left' at the beginning and 'right' at the end of the array.
        int left = 0;
        int right = s.size() - 1;

        // Loop until the two pointers meet or cross each other.
        while (left < right) {
            // Swap the characters at the left and right pointers.
            swap(s[left], s[right]);

            // Move the pointers towards the center of the array.
            left++;
            right--;
        }
    }
};

Reverse String in TypeScript

/**
 Do not return anything, modify s in-place instead.
 */
function reverseString(s: string[]): void {
    // Initialize two pointers: 'left' at the beginning and 'right' at the end of the array.
    let left = 0;
    let right = s.length - 1;

    // Loop until the two pointers meet or cross each other.
    while (left < right) {
        // Swap the elements at the left and right pointers using array destructuring.
        [s[left], s[right]] = [s[right], s[left]];

        // Move the pointers towards the center of the array.
        left++;
        right--;
    }
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.