Retour aux défis
Facile 0 résolus 0 % de réussite

Palindrome String

Check whether text reads the same forwards and backwards.

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

0 ≤ length ≤ 10,000

Exemples

Exemple 1
Entrée :
text = "Racecar"

Sortie attendue :
expected = True
Entrée :
std::string text = "Racecar";

Sortie attendue :
bool expected = true;
Entrée :
string text = "Racecar";

Sortie attendue :
bool expected = true;

Explication

Lowercased, "racecar" reads the same both ways.

Exemple 2
Entrée :
text = "hello"

Sortie attendue :
expected = False
Entrée :
std::string text = "hello";

Sortie attendue :
bool expected = false;
Entrée :
string text = "hello";

Sortie attendue :
bool expected = false;

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.