Programming & Development / April 10, 2025

LeetCode 344: Reverse String โ€“ Mastering Two-Pointer Techniques

LeetCode 344 reverse string two-pointer technique in-place reversal Python array manipulation string algorithms character array beginner-friendly string reverse

๐Ÿงฉ Problem Statement

Write a function that reverses a string. The input string is given as a character array s, and you must reverse it in-place with O(1) extra memory.

๐Ÿ”ข Example

python

Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

โœ… Constraints

  • 1 <= s.length <= 10โต
  • s[i] is a printable ASCII character.
  • Must be done in-place (i.e., modify the original array, not return a new one).

๐Ÿ’ก Key Concept: Two-Pointer Swapping

This is a classic two-pointer problem, often used to reverse arrays or strings. The idea is simple:

  • Initialize two pointers, left = 0 and right = len(s) - 1
  • Swap s[left] with s[right]
  • Move both pointers toward the center
  • Repeat until they meet or cross

โœ… Python Code โ€“ Two-Pointer Method (In-place)

python

class Solution:
    def reverseString(self, s: List[str]) -> None:
        left, right = 0, len(s) - 1

        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1
๐Ÿง  Note: Since this is done in-place, there is no return statement. The original array s is directly modified.

๐Ÿ” Step-by-Step Example

For s = ["h", "e", "l", "l", "o"]:

StepLeftRights104["o", "e", "l", "l", "h"]213["o", "l", "l", "e", "h"]322Pointers meet โ€“ done!

๐Ÿงช Alternative: Pythonic One-liner (Not allowed in interviews where in-place is required)

python

s[:] = s[::-1]  # This also modifies the list in-place

But this uses slicing and may not be accepted in all interview scenarios.

โฑ๏ธ Time and Space Complexity

MetricValueTime ComplexityO(n)Space ComplexityO(1)

Since you only use a few pointers and swap elements directly, space remains constant.

๐Ÿ“ฆ Extra Test Cases

python

Input: ["A"] โ†’ Output: ["A"]
Input: ["a", "b"] โ†’ Output: ["b", "a"]
Input: ["1","2","3","4"] โ†’ Output: ["4","3","2","1"]

๐Ÿš€ Use Cases

  • Reversing strings in-place in embedded systems
  • Data cleaning pipelines
  • UI text rendering in reverse languages (RTL)

โœ… Conclusion

LeetCode 344: Reverse String is a great entry-level problem to understand in-place operations and the two-pointer pattern. It builds the foundation for more complex tasks like reversing substrings, linked lists, or even matrix diagonals.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex