Statement#

Write a function that takes a string, s, as an input and determines whether or not it is a palindrome.

Note: A palindrome is a word, phrase, or sequence of characters that reads the same backward as forward.

Constraints:

  • 11 \leq s.length 2×105\leq 2 \times 10^5
  • The string s will contain English uppercase and lowercase letters, digits, and spaces.

Examples#

Created with Fabric.js 3.6.6 A B C B A Output TRUE Input Output Sample example 1

1 of 2

Created with Fabric.js 3.6.6 Input Output A B C C A Output FALSE Sample example 2

2 of 2

Understand the problem#

Let’s take a moment to make sure you've correctly understood the problem. The quiz below helps you check if you're solving the correct problem:

Valid Palindrome

1

“abab” is a palindrome.

A)

True

B)

False

Question 1 of 40 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding on how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Initialize two pointers at the beginning and end of the string.

Check whether or not the current pair of characters is identical.

If they are not identical, return FALSE. Otherwise, move both pointers by one index toward the middle.

Keep traversing them toward the middle until they meet.

If we reach the middle of the string without finding a mismatch, return TRUE.


Try it yourself#

Implement your solution in main.py in the following coding playground. We have provided a useful code template in the other file that you can use to solve this problem.

Python
main.py
two_pointers.py
Input #1
Valid Palindrome

Two Pointers: Introduction

Solution: Valid Palindrome