Problème
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
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.
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.