LeetCode Problem

How to Solve Find Mirror Score of a String

This problem requires identifying letter pairs that match their alphabetic mirrors using stack-based state tracking. Start by mapping each character to its mirror and use stacks to track unmatched letters. Efficiently managing these stacks avoids redundant comparisons and ensures a linear pass through the string while calculating the mirror score accurately.

GhostInterview Help

Need help with Find Mirror Score of a String without spending extra time grinding it?

GhostInterview can read Find Mirror Score of a String from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #3412Stack-based state managementReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Stack-based state management
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem requires identifying letter pairs that match their alphabetic mirrors using stack-based state tracking. Start by mapping each character to its mirror and use stacks to track unmatched letters. Efficiently managing these stacks avoids redundant comparisons and ensures a linear pass through the string while calculating the mirror score accurately.

Problem Statement

Given a string s of lowercase English letters, compute its mirror score. The mirror of a letter is defined as its corresponding letter in the reversed alphabet: 'a' maps to 'z', 'b' maps to 'y', and so on.

Initially, all characters in s are unmarked. For each character, find if a previously unmarked character exists such that it forms a mirror pair. Count all such valid mirror pairs to return the total mirror score.

Examples

Example 1

Input: s = "aczzx"

Output: 5

Example 2

Input: s = "abcdef"

Output: 0

For each index i , there is no index j that satisfies the conditions.

Constraints

  • 1 <= s.length <= 105
  • s consists only of lowercase English letters.

Solution Approach

Map each letter to its mirror

Create a simple hash table mapping every lowercase letter to its alphabetic mirror. This prepares direct lookup for each character during the stack processing, reducing repeated computations.

Use stacks to track unmatched letters

Maintain a stack for each character to track its unpaired positions. When encountering a character, check if its mirror stack is non-empty. If so, pop from the mirror stack and increment the score; otherwise, push the current character onto its stack.

Single-pass computation

Iterate through the string once, updating stacks and the score on the fly. This stack-based simulation avoids nested loops and guarantees efficient linear-time execution while keeping space proportional to the number of unique letters.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(n) for n = s.length due to the single-pass iteration and constant-time stack operations. Space complexity is O(1) relative to input size because only 26 stacks for lowercase letters are maintained.

What Interviewers Usually Probe

  • Focus on the stack-based state management pattern for mirror matching.
  • Ask about handling unmarked letters and simultaneous updates to stacks.
  • Probe edge cases with repeated letters or no mirror pairs.

Common Pitfalls or Variants

Common pitfalls

  • Failing to map letters correctly to their mirror equivalents.
  • Forgetting to check the corresponding mirror stack before pushing the current character.
  • Using nested loops instead of stacks, leading to O(n^2) performance.

Follow-up variants

  • Compute mirror scores with uppercase and lowercase letters combined.
  • Modify the problem to allow partial mirror pairs with a tolerance of one mismatch.
  • Calculate maximum mirror score after deleting at most k characters from s.

How GhostInterview Helps

  • Provides a guided step-by-step stack simulation to pair letters efficiently.
  • Highlights correct mirror mapping to avoid logical errors.
  • Tracks incremental score updates to prevent miscounting and ensure accurate results.

Topic Pages

FAQ

What exactly is the mirror score of a string?

The mirror score is the total number of letter pairs in the string where each letter matches the alphabetic mirror of another unmarked letter.

How do stacks help in computing mirror score?

Stacks maintain the positions of unmatched letters, enabling O(1) pairing checks with incoming characters instead of scanning the string repeatedly.

Can this approach handle very long strings efficiently?

Yes, because the algorithm iterates through the string once and uses a fixed number of stacks, ensuring linear time and constant extra space.

What should I watch out for when mapping letters to mirrors?

Ensure the mapping correctly pairs 'a' with 'z', 'b' with 'y', etc., and consistently uses lowercase letters for indexing stacks.

Is the stack-based state management pattern always needed for mirror score problems?

For this problem, it is essential to efficiently track unmatched letters and pair mirrors without scanning the entire string repeatedly.

GhostInterview Solver

Need direct help with Find Mirror Score of a String instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Find Mirror Score of a String from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.