↓Skip to main content
  1. LeetCode/

LeetCode 917: Reverse Only Letters

·2 mins· ·
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-09-25

  • Submissions: Wrong Answer → Runtime Error ×3 → Time Limit Exceeded ×2 → Wrong Answer ×2 → Runtime Error → Wrong Answer → Runtime Error → Wrong Answer ×9 → Runtime Error → Wrong Answer → Accepted → Wrong Answer ×2 → Accepted ×2

Approach
#

The same two pointers as LC 125 (Two Pointers Template 1): move left past non-letters, move right past non-letters, and swap only when both are letters. Python strings are immutable, so convert to a list and join at the end.

My Attempts
#

This took 27 submissions, the most in this batch:

  • Attempt 1 started both pointers at 0 (i = j = 0), so the loop never ran, and it tried to swap characters inside a string.
  • Then came many “store the letters and fill them back” variants with off-by-one errors: a single "z" returned an empty string or "zz", and "a7" lost its letter, returning "7".
  • The 2026 rewrite with two pointers got it right in one go.

Solution
#

class Solution:
    def reverseOnlyLetters(self, s: str) -> str:
        a = list(s)
        l, r = 0, len(a) - 1
        while l < r:
            if not a[l].isalpha():
                l += 1
            elif not a[r].isalpha():
                r -= 1
            else:
                a[l], a[r] = a[r], a[l]
                l, r = l + 1, r - 1
        return "".join(a)

Complexity
#

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

Mistakes to Avoid
#

  • Strings cannot be modified in place; use a list.
  • The pointers start at the head and the tail.

Interview Explanation
#

I convert to a list and move two pointers that skip non-letters, swapping when both are letters, then join back. Time O(n).

Related

LeetCode 925: Long Pressed Name
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 925 note: one pointer each over name and typed; every typed character either matches the next name character or repeats the previous typed character
LeetCode 821: Shortest Distance to a Character
·2 mins
LeetCode Easy Array Two-Pointers String
LeetCode 821 note: one left-to-right pass for the distance to the nearest c on the left, then a right-to-left pass taking the minimum
LeetCode 844: Backspace String Compare
·2 mins
LeetCode Easy Two-Pointers String Stack Simulation
LeetCode 844 note: simulate backspaces with a stack; for O(1) space, walk from the end and count how many characters each # skips
LeetCode 680: Valid Palindrome II
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 680 note: at the first mismatch the only options are deleting the left or the right character; check whether either remainder is a palindrome
LeetCode 696: Count Binary Substrings
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 696 note: split the string into runs of equal characters; each adjacent pair of runs contributes min(previous, current) substrings
LeetCode 541: Reverse String II
·1 min
LeetCode Easy Two-Pointers String
LeetCode 541 note: in every block of 2k reverse the first k; when fewer than k remain reverse them all, which slicing handles automatically