NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 125. Valid PalindromeEasy
  • 13. Roman to IntegerEasy
  • 14. Longest Common PrefixEasy
  • 20. Valid ParenthesesEasy
  • 26. Remove Duplicates from Sorted ArrayEasy

344. Reverse String

Easy
StringTwo Pointers
Answered: Nov 28, 2022
View on LeetCode

Problem Description

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

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.

💡 Hints (1)


Solutions

Complexity Analysis

Time Complexity:O(n)

The reverse method iterates through the array once to reverse it, resulting in a time complexity of O(n)O(n)O(n), where n is the length of the array s.

Space Complexity:O(1)

Since the reverse method modifies the array in place, it does not require any additional space beyond a few temporary variables used internally by the method.

The built-in reverse() method reverses an array in place. It modifies the original array and returns a reference to the same array. In this case, we simply call s.reverse() to reverse the input array of characters.

Complexity Analysis

Time Complexity:O(n)

Where n is the length of the array s. We need to iterate through half of the array to swap the characters.

Space Complexity:O(1)

Since we are modifying the array in place and only using a few temporary variables, the space complexity is O(1)O(1)O(1).

In this approach, we use two pointers: one starting at the beginning of the array (left) and the other at the end of the array (right). We swap the characters at these two pointers using a temporary variable temp to hold one of the characters during the swap. After swapping, we move the left pointer one step to the right and the right pointer one step to the left. We continue this process until the left pointer is no longer less than the right pointer, which means we have reversed the entire array.

Complexity Analysis

Time Complexity:O(n)

Where n is the length of the array s. We need to iterate through half of the array to swap the characters.

Space Complexity:O(1)

Since we are modifying the array in place and only using a few temporary variables, the space complexity is O(1)O(1)O(1).

In this approach, we use two pointers: one starting at the beginning of the array (left) and the other at the end of the array (right). We swap the characters at these two pointers and then move the left pointer one step to the right and the right pointer one step to the left. We continue this process until the left pointer is no longer less than the right pointer, which means we have reversed the entire array.

Complexity Analysis

Time Complexity:O(n)

Where n is the length of the array s. We need to iterate through half of the array to swap the characters.

Space Complexity:O(n)

Due to the recursive call stack. In the worst case, the depth of the recursion can be equal to the length of the array if the array is large.

In this approach, we define a helper function reverse that takes the array and two pointers (left and right). The function checks if the left pointer is greater than or equal to the right pointer, in which case it returns (base case). Otherwise, it swaps the characters at the left and right pointers and then recursively calls itself with the left pointer moved one step to the right and the right pointer moved one step to the left. This process continues until the base case is reached, effectively reversing the array.

Complexity Analysis

Time Complexity:O(n)

Where n is the length of the array s. We need to iterate through the array twice: once to push characters onto the stack and once to pop characters from the stack and overwrite the original array.

Space Complexity:O(n)

Since we are using a stack to store all characters of the input array, the space complexity is O(n)O(n)O(n), where n is the length of the array s.

In this approach, we use a stack data structure to reverse the string. We first push all characters of the input array s onto the stack. Then, we pop characters from the stack and overwrite the original array s with the popped characters. This effectively reverses the array since the last character pushed onto the stack will be the first one popped off.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Using Built-in Reverse Method
O(n)O(n)O(n)O(1)O(1)O(1)Simplest and most concise — single line of codeRelies on language built-in, may not be accepted in interviews
Two Pointers with Temporary Variable
O(n)O(n)O(n)O(1)O(1)O(1)Classic interview approach, easy to understand, demonstrates core algorithmRequires a temporary variable for swapping
Two Pointers with Array Destructuring
O(n)O(n)O(n)O(1)O(1)O(1)Eliminates temporary variable using destructuring syntaxMay be less readable for beginners
Recursive Approach
O(n)O(n)O(n)O(n)O(n)O(n)Elegant recursive logicUses O(n)O(n)O(n) stack space, risk of stack overflow on large inputs
Using Stack
O(n)O(n)O(n)O(n)O(n)O(n)Demonstrates stack data structure usageRequires O(n)O(n)O(n) extra space for the stack, two passes needed

The optimal approach for this problem is Solution 2: Two Pointers with Temporary Variable, which achieves O(n)O(n)O(n) time complexity and O(1)O(1)O(1) space complexity. It is the classic two-pointer approach that clearly demonstrates the core algorithm, making it ideal for interviews and easy to understand.


Previous283. Move Zeroes
Next347. Top K Frequent Elements
Related posts
  • 125. Valid PalindromeEasy
  • 13. Roman to IntegerEasy
  • 14. Longest Common PrefixEasy
  • 20. Valid ParenthesesEasy
  • 26. Remove Duplicates from Sorted ArrayEasy
On this page
6 lines
1class Solution:
2 def reverseString(self, s: List[str]) -> None:
3 """
4 Do not return anything, modify s in-place instead.
5 """
6 s.reverse()
15 lines
1class Solution:
2 def reverseString(self, s: List[str]) -> None:
3 """
4 Do not return anything, modify s in-place instead.
5 """
6 left = 0
7 right = len(s) - 1
8
9 while left < right:
10 temp = s[left]
11 s[left] = s[right]
12 s[right] = temp
13
14 left += 1
15 right -= 1
14 lines
1class Solution:
2 def reverseString(self, s: List[str]) -> None:
3 """
4 Do not return anything, modify s in-place instead.
5 """
6 left = 0
7 right = len(s) - 1
8
9 while left < right:
10 # Swap characters at left and right pointers using Tuple unpacking
11 s[left], s[right] = s[right], s[left]
12
13 left += 1
14 right -= 1
16 lines
1class Solution:
2 def reverseString(self, s: List[str]) -> None:
3 """
4 Do not return anything, modify s in-place instead.
5 """
6 self.reverse(s, 0, len(s) - 1)
7
8 def reverse(self, stringArray: List[str], left: int, right: int) -> None:
9 if left >= right:
10 return
11
12 temp = stringArray[left]
13 stringArray[left] = stringArray[right]
14 stringArray[right] = temp
15
16 self.reverse(stringArray, left + 1, right - 1)
14 lines
1class Solution:
2 def reverseString(self, s: List[str]) -> None:
3 """
4 Do not return anything, modify s in-place instead.
5 """
6 stack = []
7
8 # Push all characters onto the stack
9 for char in s:
10 stack.append(char)
11
12 # Pop characters from the stack and overwrite the original array
13 for i in range(len(s)):
14 s[i] = stack.pop()