↓Skip to main content
  1. LeetCode/

LeetCode 3823: Reverse Letters Then Special Characters in a String

·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: 2026-01-31

  • Submissions: Accepted (first try)

Approach
#

Reverse the letters back into the letter positions, then reverse the special characters back into theirs.

Each pass is “reverse while skipping the other kind of character” with converging pointers: each side skips ahead to a character of the kind being processed, they swap, and both move inward. The two passes touch disjoint positions, so their order does not matter.

My Solution
#

class Solution:
    def reverseByType(self, s: str) -> str:
        chars = list(s)

        def reverse(keep) -> None:
            left, right = 0, len(chars) - 1
            while left < right:
                while left < right and not keep(chars[left]):
                    left += 1
                while left < right and not keep(chars[right]):
                    right -= 1
                chars[left], chars[right] = chars[right], chars[left]
                left += 1
                right -= 1

        reverse(str.isalpha)
        reverse(lambda ch: not ch.isalpha())
        return "".join(chars)

My original wrote the two passes out separately with opposite conditions. Here “which kind to process” is a parameter, so both passes share one function.

Complexity
#

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

Mistakes to Avoid
#

  • Inner skipping loops without a left < right check: the pointers can cross the middle and swap the wrong positions.
  • Modifying the string directly: convert it to a list first.

Interview Explanation
#

I run two converging-pointer reversals: the first swaps only letters, with each pointer skipping non-letters; the second swaps only special characters. The passes touch disjoint positions, so they can be done separately. One reversal function takes the predicate as a parameter, and the total is O(n).

Related

LeetCode 2697: Lexicographically Smallest Palindrome
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 2697 note: converging pointers, setting both ends to the smaller character whenever they differ
LeetCode 3794: Reverse String Prefix
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 3794 note: one line with slicing; a hand-written two-pointer version must convert the string to a list first
LeetCode 3750: Minimum Number of Flips to Reverse Binary String
·2 mins
LeetCode Easy Two-Pointers Bit-Manipulation String
LeetCode 3750 note: compare the binary string with its reverse bit by bit; the number of mismatches is the answer
LeetCode 2000: Reverse Prefix of Word
·1 min
LeetCode Easy Two-Pointers String Stack
LeetCode 2000 note: find the first index i of ch, reverse word[:i + 1] and append the rest; return the word unchanged if ch is absent
LeetCode 942: DI String Match
·1 min
LeetCode Easy Array Two-Pointers String Greedy
LeetCode 942 note: two pointers hold the smallest and largest unused values; place the smallest on I and the largest on D
LeetCode 557: Reverse Words in a String III
·1 min
LeetCode Easy Two-Pointers String
LeetCode 557 note: split on spaces, reverse each word, and join with spaces