Mirror characters of a string
Given a string s and an integer k. The task is to perform a mirror transformation on the substring starting from the k-th position to the end of the string. In this transformation, each letter is replaced by its corresponding mirrored letter in the English alphabet ('a' -> 'z', 'b' -> 'y', 'c' -> 'x', etc.).
Examples:
Input: s = geeksforgeeks, k = 5
Output: geekstulivtvh
Explanation: Characters from the 5th position (s) to the end -> sforgeeks
Applying the mirror operation: s -> h, f -> u, o -> l, r -> i, g -> t, e -> v, e -> v, k -> p, s -> hInput: s = abcdef, k = 3
Output: abxwvu
Explanation: Characters from the 3rd position (c) to the end -> cdef
Applying the mirror operation: c -> x, d -> w, e -> v, f -> uInput: s = hello, k = 2
Output: hsvool
Explanation: Characters from the 2nd position (e) to the end -> ello
Applying the mirror operation: e -> v, l -> o, l -> o, o -> l
Table of Content
Using Reversed Alphabet String - O(n) Time and O(26) Space
The idea is to mirror transform characters from the k-th position to the end of the string. Instead of computing the mirrored character dynamically, we use a precomputed reversed alphabet mapping stored in a string, where 'a' maps to 'z', 'b' to 'y', and so on. By iterating over the required portion of s, we replace each character using this lookup table, ensuring an efficient transformation.
Below is the implementation of the above approach:
// C++ code to perform mirror transformation
// on a string from kth position to end
#include <bits/stdc++.h>
using namespace std;
string mirrorTransform(string s, int k) {
// Mirror mapping for lowercase letters
string mirror = "zyxwvutsrqponmlkjihgfedcba";
// Apply the transformation from k-th position
// to the end
for (int i = k - 1; i < s.length(); i++) {
s[i] = mirror[s[i] - 'a'];
}
return s;
}
int main() {
string s = "geeksforgeeks";
int k = 5;
cout << mirrorTransform(s, k) << endl;
return 0;
}
// Java code to perform mirror transformation
// on a string from kth position to end
import java.util.*;
class GfG {
static String mirrorTransform(String s, int k) {
// Mirror mapping for lowercase letters
String mirror = "zyxwvutsrqponmlkjihgfedcba";
// Apply the transformation from k-th position
// to the end
char[] arr = s.toCharArray();
for (int i = k - 1; i < arr.length; i++) {
arr[i] = mirror.charAt(arr[i] - 'a');
}
return new String(arr);
}
public static void main(String[] args) {
String s = "geeksforgeeks";
int k = 5;
System.out.println(mirrorTransform(s, k));
}
}
# Python code to perform mirror transformation
# on a string from kth position to end
def mirrorTransform(s, k):
# Mirror mapping for lowercase letters
mirror = "zyxwvutsrqponmlkjihgfedcba"
# Apply the transformation from k-th position
# to the end
s = list(s)
for i in range(k - 1, len(s)):
s[i] = mirror[ord(s[i]) - ord('a')]
return "".join(s)
if __name__ == "__main__":
s = "geeksforgeeks"
k = 5
print(mirrorTransform(s, k))
// C# code to perform mirror transformation
// on a string from kth position to end
using System;
class GfG {
static string mirrorTransform(string s, int k) {
// Mirror mapping for lowercase letters
string mirror = "zyxwvutsrqponmlkjihgfedcba";
// Apply the transformation from k-th position
// to the end
char[] arr = s.ToCharArray();
for (int i = k - 1; i < arr.Length; i++) {
arr[i] = mirror[arr[i] - 'a'];
}
return new string(arr);
}
public static void Main() {
string s = "geeksforgeeks";
int k = 5;
Console.WriteLine(mirrorTransform(s, k));
}
}
// JavaScript code to perform mirror transformation
// on a string from kth position to end
function mirrorTransform(s, k) {
// Mirror mapping for lowercase letters
let mirror = "zyxwvutsrqponmlkjihgfedcba";
// Apply the transformation from k-th position
// to the end
let arr = s.split("");
for (let i = k - 1; i < arr.length; i++) {
arr[i] = mirror[arr[i].charCodeAt(0) - 'a'.charCodeAt(0)];
}
return arr.join("");
}
let s = "geeksforgeeks";
let k = 5;
console.log(mirrorTransform(s, k));
Output
geekhulitvvph
Time Complexity: O(n), where n represents the size of the given string.
Auxiliary Space: O(26), since we use a fixed-size mirror string of size 26 and modify in place.
Using No Extra Space - O(n) Time and O(1) Space
The idea is to perform a mirror transformation without creating an extra string. For that, instead of creating a mirror string, we use a direct formula: s[i] = 'a' + ('z' - s[i]), which effectively replaces each letter with its mirrored counterpart in the alphabet.
Below is the implementation of the above approach:
// C++ code to perform mirror transformation
// without creating mirror string
#include <bits/stdc++.h>
using namespace std;
string mirrorTransform(string s, int k) {
// Apply the transformation from k-th position
// to the end
for (int i = k - 1; i < s.length(); i++) {
s[i] = 'a' + ('z' - s[i]);
}
return s;
}
int main() {
string s = "geeksforgeeks";
int k = 5;
cout << mirrorTransform(s, k) << endl;
return 0;
}
// Java code to perform mirror transformation
// without creating mirror string
class GfG {
static String mirrorTransform(String s, int k) {
// Apply the transformation from k-th position
// to the end
char[] arr = s.toCharArray();
for (int i = k - 1; i < arr.length; i++) {
arr[i] = (char) ('a' + ('z' - arr[i]));
}
return new String(arr);
}
public static void main(String[] args) {
String s = "geeksforgeeks";
int k = 5;
System.out.println(mirrorTransform(s, k));
}
}
# Python code to perform mirror transformation
# without creating mirror string
def mirrorTransform(s, k):
# Apply the transformation from k-th position
# to the end
s = list(s)
for i in range(k - 1, len(s)):
s[i] = chr(ord('a') + (ord('z') - ord(s[i])))
return "".join(s)
if __name__ == "__main__":
s = "geeksforgeeks"
k = 5
print(mirrorTransform(s, k))
// C# code to perform mirror transformation
// without creating mirror string
using System;
class GfG {
static string mirrorTransform(string s, int k) {
// Apply the transformation from k-th position
// to the end
char[] arr = s.ToCharArray();
for (int i = k - 1; i < arr.Length; i++) {
arr[i] = (char) ('a' + ('z' - arr[i]));
}
return new string(arr);
}
static void Main() {
string s = "geeksforgeeks";
int k = 5;
Console.WriteLine(mirrorTransform(s, k));
}
}
// JavaScript code to perform mirror transformation
// without creating mirror string
function mirrorTransform(s, k) {
// Apply the transformation from k-th position
// to the end
let arr = s.split("");
for (let i = k - 1; i < arr.length; i++) {
arr[i] = String.fromCharCode(
'a'.charCodeAt(0) + ('z'.charCodeAt(0) - arr[i].charCodeAt(0))
);
}
return arr.join("");
}
let s = "geeksforgeeks";
let k = 5;
console.log(mirrorTransform(s, k));
Output
geekhulitvvph
Time Complexity: O(n), as each character from index k-1 to the end of the string is processed once, making it O(n).
Space Complexity: O(1), as the transformation is done in-place, so no extra space is used.