↓Skip to main content
  1. LeetCode/

LeetCode 344: Reverse String

·1 min· ·
LeetCode Easy Two-Pointers String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-23

  • Submissions: Accepted ×2 → Runtime Error → Accepted

Approach
#

Swap with two pointers and move inward until they meet (Two Pointers Template 1).

My Attempts
#

A 2026 rewrite started the right pointer at len(s), out of range.

Solution
#

class Solution:
    def reverseString(self, s: List[str]) -> None:
        l, r = 0, len(s) - 1
        while l < r:
            s[l], s[r] = s[r], s[l]
            l, r = l + 1, r - 1

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • The right pointer starts at len(s) - 1.

Interview Explanation
#

Two pointers swap the ends and move inward, reversing in place. Time O(n), space O(1).

Related

LeetCode 392: Is Subsequence
·2 mins
LeetCode Easy Two-Pointers String Dynamic-Programming
LeetCode 392 note: two pointers; the pointer on t always advances, and the one on s advances on a match; bound-check both
LeetCode 125: Valid Palindrome
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 125 note: two pointers skip non-alphanumeric characters and compare lowercase letters, with digits included
LeetCode 28: Find the Index of the First Occurrence in a String
·1 min
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 note: try matching needle at every start, or use str.find; index raises when not found
LeetCode 345: Reverse Vowels of a String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 345 note: the same two pointers as LC 917, with the condition switched to “is a vowel” (both cases)
LeetCode 283: Move Zeroes
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 283 note: the write pointer advances only on non-zero values, swapping with them, which pushes zeros to the back
LeetCode 290: Word Pattern
·1 min
LeetCode Easy Hash-Table String
LeetCode 290 note: pattern characters and words must correspond one to one; use two dicts or compare set sizes