Mirror phrase
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
A phrase is a mirror phrase if it reads the same forwards and backwards once you ignore everything except letters and digits, and treat capital and small letters as the same.
For example, Step on no pets! is a mirror phrase: keeping only letters and digits, in small letters, gives steponnopets, which is the same backwards.
Given text, return true if it is a mirror phrase, and false if not.
Examples
Example 1
- Input
- text = "Step on no pets!"
- Output
- true
Letters only, in small letters: steponnopets. Backwards it's the same.
Example 2
- Input
- text = "Never odd or even"
- Output
- true
neveroddoreven reads the same both ways.
Example 3
- Input
- text = "Mirror"
- Output
- false
mirror backwards is rorrim.
Rules
texthas between 0 and 200,000 characters: letters, digits, spaces and punctuation.- Letters are the English letters a to z, in either case.
- Text with no letters or digits at all counts as a mirror phrase.
Words on this page, in plain English (2)Open if any word is new to you
- String
- A piece of text, written between quotes. Each letter in it is a character.
- Boolean (true or false)
- A value that can only be
trueorfalse. Like a light switch: on or off.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
We ignore everything that isn't a letter or a digit, and we don't care about capitals. What's left must read the same in both directions. Spaces and punctuation don't count: Never odd or even is a mirror phrase.
2. Solve an example by hand
With Step on no pets!, compare the first letter with the last: S and s, same (ignoring capitals). Next pair inward: t and t. Then e and e, p and p. When you hit the space after "Step", you just skip it. Every pair matches, so it's a mirror phrase.
You were moving inward from both ends at once: two pointers.
3. The simple way
Clean the text, then compare it with its reverse:
function isMirrorPhrase(text) { const clean = text.toLowerCase().replace(/[^a-z0-9]/g, ""); return clean === [...clean].reverse().join("");}This is correct, readable, and O(n). It's a fine answer! The downside is memory: it builds a cleaned copy and a reversed copy of the text. With two pointers we can check the text where it is, using O(1) extra memory. Learning that version teaches the pattern.
4. Spot the pattern
"Compare the first with the last, then the second with the second-last…" is the from both ends template of two pointers.
5. The two-pointer idea
Keep left at the start and right at the end. While they haven't met:
- If
leftis on something that isn't a letter or digit, move it forward. - Else if
rightis, move it back. - Else compare the two characters in small letters. Different? Not a mirror phrase. Same? Move both inward.
Two pointers, meeting in the middle
text
function isMirrorPhrase(text) { let left = 0; let right = text.length - 1; while (left < right) { if (!isLetterOrDigit(text[left])) { left++; } else if (!isLetterOrDigit(text[right])) { right--; } else if (text[left].toLowerCase() !== text[right].toLowerCase()) { return false; } else { left++; right--; } } return true;} function isLetterOrDigit(char) { return /[a-z0-9]/i.test(char);}Put left on the first character and right on the last.
6. The code, line by line
let left = 0; let right = text.length - 1;Two pointers:
lefton the first character,righton the last (text.length - 1, because positions start at 0).while (left < right) {Keep going until the pointers meet. When
left < rightstops being true, every pair has been checked; a middle character on its own always matches itself.if (!isLetterOrDigit(text[left])) { left++; } else if (!isLetterOrDigit(text[right])) { right--;Skip anything that isn't a letter or digit. Only one pointer moves per step, which keeps the logic simple.
} else if (text[left].toLowerCase() !== text[right].toLowerCase()) { return false;Both pointers are on letters or digits. Compare them in small letters with
toLowerCase(); any difference means it's not a mirror phrase.} else { left++; right--; }They match: move both pointers inward.
function isLetterOrDigit(char) { return /[a-z0-9]/i.test(char);}A helper that tests one character.
/[a-z0-9]/iis a pattern meaning "a letter or digit"; theimakes it ignore capitals.
7. How fast is it?
| Clean and reverse | Two pointers | |
|---|---|---|
| Time | O(n) | O(n): each pointer moves at most n times |
| Extra memory | O(n): two copies | O(1): two numbers |
Common mistakes
- Comparing before skipping. If you compare
text[left]andtext[right]while one is a space,"a b a"looks broken. - Forgetting digits.
7xis not a mirror phrase:7andxare different, and digits count. - Moving both pointers when skipping. Skip only the pointer that's on punctuation; the other one may be on a letter that still needs checking.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Mirror phrase
Read the 20 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function isMirrorPhrase(text) { let left = 0; let right = text.length - 1; while (left < right) { if (!isLetterOrDigit(text[left])) { left++; } else if (!isLetterOrDigit(text[right])) { right--; } else if (text[left].toLowerCase() !== text[right].toLowerCase()) { return false; } else { left++; right--; } } return true;} function isLetterOrDigit(char) { return /[a-z0-9]/i.test(char);}