Given a string s 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".
Table of Content
[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.
#include <iostream>
using namespace std;
string transformString(string s)
{
string ans = "";
for (char ch : s)
{
if (ch >= 'a' && ch <= 'z')
{
ans += ('z' - (ch - 'a'));
}
else if (ch >= 'A' && ch <= 'Z')
{
ans += ('Z' - (ch - 'A'));
}
}
return ans;
}
// Driver Code
int main()
{
string s = "GfG";
cout << transformString(s) << endl;
return 0;
}
import java.util.*;
public class GfG {
public static String transformString(String s)
{
String ans = "";
for (char ch : s.toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
ans += (char)('z' - (ch - 'a'));
}
else if (ch >= 'A' && ch <= 'Z') {
ans += (char)('Z' - (ch - 'A'));
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
String s = "GfG";
System.out.println(transformString(s));
}
}
def transformString(s):
ans = ""
for ch in s:
if 'a' <= ch <= 'z':
ans += chr(ord('z') - (ord(ch) - ord('a')))
elif 'A' <= ch <= 'Z':
ans += chr(ord('Z') - (ord(ch) - ord('A')))
return ans
# Driver Code
if __name__ == "__main__":
s = "GfG"
print(transformString(s))
using System;
public class GfG {
public static string transformString(string s)
{
string ans = "";
foreach(char ch in s)
{
if (ch >= 'a' && ch <= 'z') {
ans += (char)('z' - (ch - 'a'));
}
else if (ch >= 'A' && ch <= 'Z') {
ans += (char)('Z' - (ch - 'A'));
}
}
return ans;
}
// Driver Code
public static void Main()
{
string s = "GfG";
Console.WriteLine(transformString(s));
}
}
function transformString(s) {
let ans = "";
for (let ch of s) {
if (ch >= 'a' && ch <= 'z') {
ans += String.fromCharCode('z'.charCodeAt(0) - (ch.charCodeAt(0) - 'a'.charCodeAt(0)));
}
else if (ch >= 'A' && ch <= 'Z') {
ans += String.fromCharCode('Z'.charCodeAt(0) - (ch.charCodeAt(0) - 'A'.charCodeAt(0)));
}
}
return ans;
}
// Driver Code
let s = "GfG";
console.log(transformString(s));
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.
#include <iostream>
using namespace std;
string transformString(string s)
{
// Transform each character according to its position in the reversed English
// alphabet.
for (char &ch : s)
{
// Lowercase character.
if (ch >= 'a' && ch <= 'z')
ch = 'z' - (ch - 'a');
// Uppercase character.
else
ch = 'Z' - (ch - 'A');
}
return s;
}
// Driver Code
int main()
{
string s = "GfG";
cout << transformString(s) << endl;
return 0;
}
import java.util.*;
public class GfG {
public static String transformString(String s)
{
// Transform each character according to its
// position in the reversed English alphabet.
char[] charArray = s.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char ch = charArray[i];
// Lowercase character.
if (ch >= 'a' && ch <= 'z')
charArray[i] = (char)('z' - (ch - 'a'));
// Uppercase character.
else
charArray[i] = (char)('Z' - (ch - 'A'));
}
return new String(charArray);
}
public static void main(String[] args)
{
String s = "GfG";
System.out.println(transformString(s));
}
}
def transformString(s):
# Transform each character according to its position in the reversed English
# alphabet.
s = list(s)
for i in range(len(s)):
ch = s[i]
# Lowercase character.
if 'a' <= ch <= 'z':
s[i] = chr(ord('z') - (ord(ch) - ord('a')))
# Uppercase character.
else:
s[i] = chr(ord('Z') - (ord(ch) - ord('A')))
return ''.join(s)
# Driver Code
if __name__ == "__main__":
s = "GfG"
print(transformString(s))
using System;
public class GfG {
public static string transformString(string s)
{
// Transform each character according to its
// position in the reversed English alphabet.
char[] charArray = s.ToCharArray();
for (int i = 0; i < charArray.Length; i++) {
char ch = charArray[i];
// Lowercase character.
if (ch >= 'a' && ch <= 'z')
charArray[i] = (char)('z' - (ch - 'a'));
// Uppercase character.
else
charArray[i] = (char)('Z' - (ch - 'A'));
}
return new string(charArray);
}
public static void Main()
{
string s = "GfG";
Console.WriteLine(transformString(s));
}
}
function transformString(s) {
// Transform each character according to its position in the reversed English
// alphabet.
let charArray = s.split('')
for (let i = 0; i < charArray.length; i++) {
let ch = charArray[i]
// Lowercase character.
if (ch >= 'a' && ch <= 'z')
charArray[i] = String.fromCharCode('z'.charCodeAt(0) - (ch.charCodeAt(0) - 'a'.charCodeAt(0)))
// Uppercase character.
else
charArray[i] = String.fromCharCode('Z'.charCodeAt(0) - (ch.charCodeAt(0) - 'A'.charCodeAt(0)))
}
return charArray.join('');
}
// Driver Code
let s = "GfG"
console.log(transformString(s))
Output
TuT
Time Complexity:Â O(n)
Auxiliary Space:Â O(1)