Skip to main content

1 - Valid Palindrome (Easy)

Problem Statement

Write a function that takes a string s as input and checks whether it’s a palindrome or not.

Note: A phrase, word or sequence is a palindrome that reads the same backwards as forwards.

Example 1:

Input: s = "ABCBA"
Output: True
Explanation: "ABCBA" is a palindrome.

Example 2:

Input: s = "ABCCA"
Output: False
Explanation: "ABCCA" is not a palindrome.

Constraints:

  • 1 <= s.length <= 2 * 10 5
  • The string won’t have any spaces and will only consist of ASCII characters.

Two Pointer Approch

The two-pointer approach works by comparing the characters at both ends of the string, starting from the first and last characters. If the characters are different, the string is not a palindrome and the function returns False. If the characters are the same, the pointers are moved towards each other, one step at a time, until they meet in the middle of the string.

If all the characters are the same and the pointers haven't returned False, then the string is a palindrome and the function returns True.

This approach is efficient, because it only needs to check half of the characters in the string, rather than checking each character individually.

Written by @kondekarshubham123
def is_palindrome(s):

left, right = 0, len(s) - 1

while left <= right:
if s[left] != s[right]:
return False
left += 1
right -= 1

return True

Other approches

Written by @kondekarshubham123
def is_palindrome(s):
return s==s[::-1]