Skip to content
Algorithms for Beginners

Problems / Two pointers

Mirror phrase

easy

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. 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

  1. Example 1

    Input
    text = "Step on no pets!"
    Output
    true

    Letters only, in small letters: steponnopets. Backwards it's the same.

  2. Example 2

    Input
    text = "Never odd or even"
    Output
    true

    neveroddoreven reads the same both ways.

  3. Example 3

    Input
    text = "Mirror"
    Output
    false

    mirror backwards is rorrim.

Rules

  • text has 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 true or false. Like a light switch: on or off.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function isMirrorPhrase(text) {
  // Return true or false.
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in two pointers

  • Learn the two pointers pattern

    Walk through a list from both ends (or at two speeds) at once.

  • Flip in place

    Easy. Reverse a list by swapping items inside it, without building a new list.

  • Pair in a sorted list

    Easy. Find two prices that add up to a budget, when the prices are already sorted.

  • Squash repeats

    Easy. Remove repeated values from a sorted list in place, keeping one of each.

  • Biggest tank

    Medium. Choose two walls that, with the ground between them, hold the most water.

  • Three to zero

    Medium. Find every different set of three numbers in a list that adds up to zero.