Check Palindrome Using a Loop

Easy ⏱ 8 min 74% acceptance ★★★★☆ 4.4
Write a function is_palindrome(s) that returns True if the string s reads the same forwards and backwards, checked with a loop that compares characters from both ends moving inward (do not use slicing to reverse the string).

Examples

Example 1
Input
is_palindrome("racecar")
Output
True
Explanation

Reads the same forward and backward.

Example 2
Input
is_palindrome("hello")
Output
False
Explanation

Not symmetric.

Constraints

  • s contains no spaces and comparison is case-sensitive.
  • Must not use s[::-1] or reversed().

Topics

Loopswhile loop

Companies

TCSWipro

Hints

Hint 1

Use two pointers, `left = 0` and `right = len(s) - 1`, moving toward the center.

Hint 2

Return False as soon as a mismatch is found.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.