Reverse Only Letters

You are given a string, ‘S’. You need to reverse the string where characters that are not an alphabet stay in the same place, and the rest reverse their positions.

Eg: “a-bcd” becomes “d-cba”

Input Format:
The first line of the input contains ‘T’ denoting the number of test cases.

The first line of each test case contains a string ‘S’.
Output Format:
For every test case, we need to print the reversed string in a new line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
0 <= |S| <= 5000

Where |S| denotes the length of string 'S'.

Time Limit: 1 Sec
CodingNinjas
author
2y

Key idea - 2 pointer method
Approach - place one pointer p1 on start, 2nd pointer p2 on end. Move p1 to right and p2 to left unless we find a non special character for both, if found for both, swap the...read more

CodingNinjas
author
2y
Two Pointer Approach

Approach: Just like in order to reverse a string ‘S’ = “abcd”, we can can swap(S[0], S[3]) and swap(S[1],S[2]) to get new string “dcba”.

Similarly in this problem, we do the same u...read more

Help your peers!
Add answer anonymously...
PayU Payments Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter