Easy 0 solved 0% acceptance

Palindrome String

Check whether text reads the same forwards and backwards.

What you'll learn

  • Compare characters from both ends
  • Stop early when a mismatch appears
  • Handle empty and one-character strings

Problem

Decide whether text is a palindrome, considering only letters and digits and ignoring case.

Implement is_palindrome(text).

Input

text: The text to check (ASCII characters).

Output

Return true for a palindrome, otherwise false.

Constraints

0 ≤ length ≤ 10,000

Examples

Example 1
Input:
text = "Racecar"

Expected output:
expected = True
Input:
std::string text = "Racecar";

Expected output:
bool expected = true;
Input:
string text = "Racecar";

Expected output:
bool expected = true;

Explanation

Lowercased, "racecar" reads the same both ways.

Example 2
Input:
text = "hello"

Expected output:
expected = False
Input:
std::string text = "hello";

Expected output:
bool expected = false;
Input:
string text = "hello";

Expected output:
bool expected = false;

Explanation

"hello" backwards is "olleh", which is different.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Ignore anything that is not a letter or digit before comparing.

Hint 2

Move inward from both ends, comparing case-insensitive characters.

Hint 3

Skip punctuation and spaces carefully so they do not break valid palindromes.

Expected complexity

O(n) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.