← 返回 snowflake 的题目列表Reverse Alphanumeric Segments
类型:qbank
Reverse each maximal run of alphanumeric characters (letters + digits) in place; every non-alphanumeric character (spaces, punctuation, apostrophes) stays at its original index. Example: "Hello! 2026!!! Let's dance!!!" → "olleH! 6202!!! teL's ecnad!!!".
Problem Overview
Given a string, reverse only the maximal runs of consecutive alphanumeric characters (English letters and digits). Every other character — spaces, punctuation, apostrophes — stays at its original position.
Sample Cases
Input: "Hello! 2026!!! Let's dance!!!"
Output: "olleH! 6202!!! teL's ecnad!!!"
Note that the apostrophe in Let's is not alphanumeric, so it splits the run: Let reverses to teL and the trailing s stays s, giving teL's.
Notes
A two-pointer reverse within each maximal [A-Za-z0-9]+ segment is enough; non-alphanumeric characters are fixed points you scan past.
Watch the punctuation boundaries — Let's becomes teL's, not s'teL. Digits count as alphanumeric, so 2026 reverses to 6202.