![]() |
VOOZH | about |
Given a string str, containing special characters and all the alphabets, reverse the string without affecting the positions of the special characters.
Examples:
Input: str = "a,b$c"
Output: str = "c,b$a"
Explanation: We ignore $ and , and then reverse, so answer is "c,b$a".Input: str = "A&B"
Output: "B&A"
Explanation: We ignore '&' and then reverse, so answer is "B&A".
Table of Content
The idea is to first stores all alphabet characters separately while ignoring special characters. Then, during the second traversal, the alphabets are placed back from the end of the stored array, which reverses only the letters while keeping all special characters at their original positions.
c,b$a
The intuition behind this approach is to reverse only the alphabet characters while keeping special characters fixed at their original positions. We use the two-pointer technique, where one pointer starts from the beginning and the other from the end of the string. If a pointer encounters a special character, we skip it; otherwise, we swap the alphabet characters. This efficiently reverses only the letters in a single traversal.
c,b$a