solulubeta
Sign in
DSA Basics / Warm-up
01.03 · Problem

The Mirror Nameplate

Easy Two pointers from both ends Free

A hostel is printing nameplates on glass, so every plate is also readable from behind. The warden wants to know which names look exactly the same read forwards and backwards, because those plates can be printed on plain glass and cost less.

Given a name in lowercase letters, return true if it reads the same both ways.

Examples

Example 1

Input:  name = "malayalam"
Output: true

Reading "malayalam" from the left gives m-a-l-a-y-a-l-a-m, and from the right it gives exactly the same letters, so every pair of matching positions agrees and the answer is true.

Example 2

Input:  name = "srujan"
Output: false

Example 3

Input:  name = "a"
Output: true

Constraints

name is 1 to 5,000 lowercase letters with no spaces.

Return true if the name reads the same from the left and from the right, otherwise false. A name of a single letter always reads the same.

How to solve it

Hint

Compare the first letter with the last, the second with the second-last, and keep walking inwards.

Approach: Two pointers from both ends
  1. Put one index at the start and one at the end.
  2. While they have not met, compare the two letters; if they differ, return false.
  3. Move the left index right and the right index left. If the loop finishes, return true.

Check with the example. In “malayalam” the first and last letters are both m, the next pair is a/a, then l/l… every pair matches.

Complexity: O(n) time, O(1) space.

The reference solutions in all four languages are in the Solutions tab of the workspace below.

Loading the workspace…
Mark this problem as solved when you are finished.

Item 3 of 90 · updated 2026-10-12