Replace each letter of a string with its alphabetical mirror

Quick Overview

Replace every letter of a lowercase string with its mirror in the alphabet, so a becomes z and b becomes y. The problem tests character code arithmetic, building a string in linear time, and avoiding off-by-one errors in the letter mapping.

Replace each letter of a string with its alphabetical mirror

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` consisting of lowercase English letters, return a new string where each character is replaced by its alphabetical mirror. The character `'a'` maps to `'z'`, `'b'` maps to `'y'`, `'c'` maps to `'x'`, and so on. Formally, the character at position `i` counted from `'a'` maps to the character at position `i` counted back from `'z'`. ### Function Signature ```python def mirror_characters(s: str) -> str: ``` ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters only. ### Examples ```text Input: s = "abc" Output: "zyx" ``` `'a'` becomes `'z'`, `'b'` becomes `'y'` and `'c'` becomes `'x'`, so `"abc"` becomes `"zyx"`. ```text Input: s = "hello" Output: "svool" ``` `'h'` is 7 letters after `'a'`, so it becomes `'s'`, 7 letters before `'z'`. In the same way `'e'` becomes `'v'`, each `'l'` becomes `'o'` and `'o'` becomes `'l'`. ```text Input: s = "mn" Output: "nm" ``` `'m'` and `'n'` are the middle pair of the alphabet, so they swap with each other.

Overview: Replace every letter of a lowercase string with its mirror in the alphabet, so a becomes z and b becomes y. The problem tests character code arithmetic, building a string in linear time, and avoiding off-by-one errors in the letter mapping.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s consisting of lowercase English letters, return a new string where each character is replaced by its alphabetical mirror.

The character 'a' maps to 'z', 'b' maps to 'y', 'c' maps to 'x', and so on. Formally, the character at position i counted from 'a' maps to the character at position i counted back from 'z'.

Function Signature

def mirror_characters(s: str) -> str:

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of lowercase English letters only.

Examples

Input: s = "abc"
Output: "zyx"

'a' becomes 'z', 'b' becomes 'y' and 'c' becomes 'x', so "abc" becomes "zyx".

Input: s = "hello"
Output: "svool"

'h' is 7 letters after 'a', so it becomes 's', 7 letters before 'z'. In the same way 'e' becomes 'v', each 'l' becomes 'o' and 'o' becomes 'l'.

Input: s = "mn"
Output: "nm"

'm' and 'n' are the middle pair of the alphabet, so they swap with each other.