VOOZH about

URL: https://www.geeksforgeeks.org/dsa/front-back-transformation-of-string/

⇱ Front-Back Transformation of String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Front-Back Transformation of String

Last Updated : 13 Jun, 2026

Given a string consisting only of English alphabets, transform each character by replacing it with the character present at the corresponding position in the reversed English alphabet. Thus, 'a' becomes 'z', 'b' becomes 'y', 'c' becomes 'x', and similarly 'z' becomes 'a'. Uppercase letters are also transformed in the same way while preserving their case. Return the resulting transformed string.

Examples:

Input: s = "Hello"
Output: Svool
Explanation: 'H' is replaced by 'S', 'e' by 'v', 'l' by 'o', and 'o' by 'l'. Therefore, the transformed string becomes "Svool".

Input: s = "GfG"
Output: TuT
Explanation: 'G' is replaced by 'T' and 'f' is replaced by 'u'. Hence, the resulting string is "TuT".

[Naive Approach] Traverse String and Compute Reverse Alphabet Character - O(n) Time O(n) Space

The idea is to traverse the string and generate a new transformed string by replacing each character with its corresponding character in the reversed English alphabet. Finally, return the newly formed string.


Output
TuT

Time Complexity: O(n)
Auxiliary Space: O(n)

Expected Approach - In-Place Character Transformation - O(n) Time O(1) Space

The idea is to traverse the string and directly replace each character with its corresponding character in the reversed English alphabet. Since the transformation is performed in-place, no extra space is required.


Output
TuT

Time Complexity: O(n)
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: