
At Notion, you need a utility that verifies whether a given string is a palindrome. A palindrome reads the same from left to right and right to left.
Write a function that takes a string s and returns True if s is a palindrome and False otherwise.
sTrue if s is identical to its reverseFalse otherwiseUse a direct algorithmic approach rather than relying on external libraries.
Example 1
Input: s = "racecar"
Output: True
"racecar" is the same when reversed.
Example 2
Input: s = "hello"
Output: False
The first and last characters do not match, so it is not a palindrome.
0 <= len(s) <= 10^5s contains printable ASCII charactersAn empty string should be considered a palindrome. Aim for a solution that runs in linear time.
s = "racecar"OutputTrueWhyThe characters match symmetrically from both ends: r-r, a-a, c-c, with e in the middle.s = "hello"OutputFalseWhyThe first character 'h' does not match the last character 'o'.s = "abba"OutputTrueWhyThe pairs a-a and b-b match, so the string is a palindrome.0 <= len(s) <= 10^5s contains printable ASCII charactersComparison is case-sensitiveSpaces and punctuation are treated as regular charactersdef is_palindrome(s):