Reverse Alphabetic Characters
Browserstack frequently deals with text processing, validation, and transformations, especially in scenarios like log analysis, UI element parsing, or test script manipulation. Efficient in-place string transformations are crucial for optimizing performance and resource usage. This problem challenges you to apply the two-pointer technique to selectively reverse parts of a string while maintaining the structure of other parts, a common pattern in text manipulation tasks.
Implement a function reverseAlphabetic that takes a string s as input. The function should transform the string such that only its alphabetic characters (case-sensitive) are reversed. All non-alphabetic characters (digits, symbols, spaces, etc.) must remain in their original positions within the string.
s: A string consisting of printable ASCII characters.s where only alphabetic characters are reversed, and non-alphabetic characters retain their original positions.s = "") should return an empty string.s can contain any printable ASCII character.Examples
Example 1
Input:
s = "ab-cd"
Output:
"dc-ba"
Note:
The alphabetic characters 'a', 'b', 'c', 'd' are reversed to 'd', 'c', 'b', 'a'. The hyphen '-' remains in its original position.
Example 2
Input:
s = "a-bC-dEf-ghIj"
Output:
"j-Ih-gFe-dCbA"
Note:
All alphabetic characters are reversed while non-alphabetic characters keep their positions.
Example 3
Input:
s = "Test1ng-Str1ng"
Output:
"gn1rtS-gnitTse"
Note:
Digits and hyphens are ignored. Only letters ('T','e','s','t','n','g','S','t','r','n','g') are reversed.
Constraints
1 <= s.length <= 10^5s consists of printable ASCII characters.Loading editor...