Open In App

JavaScript Program to Mirror Characters of a String

Last Updated : 07 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Our task is to mirror characters from the N-th position up to the end of a given string, where ‘a’ will be converted into ‘z’, ‘b’ into ‘y’, and so on. This JavaScript problem requires mirroring the characters in a string starting from a specified position. There are various approaches available to accomplish this, such as utilizing loops and maps. In this article, we will explore various methods to find the mirror characters of a string.

Examples:

Input : N = 3
paradox
Output : paizwlc
We mirror characters from position 3 to end.

Input : N = 6
pneumonia
Output : pnefnlmrz

Method 1: Creating a String:

In this method, we maintain a string (or a character array) containing the English lowercase alphabet. Starting from the pivot point up to the length of the string, we retrieve the reversed alphabetical counterpart of a character by using its ASCII value as an index. By applying this approach, we convert the given string into the desired mirrored form.

Syntax:

let modifiedString = "";
  
for (let i = 0; i < startPosition; i++)
    modifiedString = modifiedString + inputString[i];
  
for (let i = startPosition; i < inputStringLength; i++)
    modifiedString = modifiedString + reverseAlphabet[inputString[i].charCodeAt() - 'a'.charCodeAt()];

Example: In the code we will implement above approach by creating a string.

Javascript




function reverseAlphabetFromPosition(
        inputString, startPosition) {
    let reverseAlphabet =
        "zyxwvutsrqponmlkjihgfedcba";
    let inputStringLength =
        inputString.length;
    let newString = "";
 
    for (let i = 0; i < startPosition; i++)
        newString += inputString[i];
 
    for (let i = startPosition; i < inputStringLength; i++)
        newString += reverseAlphabet[inputString[i].
                     charCodeAt() - 'a'.charCodeAt()];
 
    return newString;
}
 
let givenString = "geeksforgeeks";
let startingPosition = 5;
console.log(
    reverseAlphabetFromPosition(
        givenString, startingPosition - 1));


Output

geekhulitvvph

Time Complexity: O(n)

Space Complexity: O(n)

Method 2: Using a for loop

  • Initialize an empty string to store the mirrored result.
  • Iterate through the characters of the given string using a loop.
  • When the loop index is equal to or greater than the starting position, replace the current character with its mirror (e.g., ‘a’ becomes ‘z’ and ‘b’ becomes ‘y’).
  • Append the mirrored character to the result string.

Syntax:

for (let i = 0; i < givenString.length; i++) {
// condition
}

Example: In the code we will implement above approach by using single for loop.

Javascript




let givenString = "geeksforgeeks";
let startingPosition = 5;
let mirroredString = '';
for (let i = 0; i < givenString.length; i++) {
    if (i >= startingPosition - 1) {
        mirroredString +=
        String.fromCharCode(219 - givenString.charCodeAt(i));
    } else {
        mirroredString += givenString[i];
    }
}
console.log(mirroredString);


Output

geekhulitvvph

Time Complexity: O(n)

Space Complexity: O(n)

Method 3 : Using Maps

  • Create a character mapping to represent the mirror transformations.
  • Initialize an empty string for the mirrored result.
  • Iterate through the characters of the given string.
  • If the character is in the mapping, replace it with the mapped character; otherwise, keep it as is.

Syntax:

const mirrorMap = {
    'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v',
    'f': 'u', 'g': 't', 'h': 's', 'i': 'r', 'j': 'q',
    'k': 'p', 'l': 'o', 'm': 'n', 'n': 'm', 'o': 'l',
    'p': 'k', 'q': 'j', 'r': 'i', 's': 'h', 't': 'g',
    'u': 'f', 'v': 'e', 'w': 'd', 'x': 'c', 'y': 'b',
    'z': 'a'
};

Example: In the code we will implement above approach by using map.

Javascript




let givenString = "geeksforgeeks";
let startingPosition = 5;
const mirrorMap = {
    'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v',
    'f': 'u', 'g': 't', 'h': 's', 'i': 'r', 'j': 'q',
    'k': 'p', 'l': 'o', 'm': 'n', 'n': 'm', 'o': 'l',
    'p': 'k', 'q': 'j', 'r': 'i', 's': 'h', 't': 'g',
    'u': 'f', 'v': 'e', 'w': 'd', 'x': 'c', 'y': 'b',
    'z': 'a'
};
 
let mirroredString = '';
for (let char of givenString) {
    startingPosition--;
    if (startingPosition > 0) {
        mirroredString += char;
    }
    else mirroredString += mirrorMap[char] || char;
}
console.log(mirroredString);


Output

geekhulitvvph

Time Complexity: O(n)

Space Complexity: O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads