Problème
Decide whether text reads the same forwards and backwards, considering only letters and digits and ignoring case. Solve it recursively: compare the outer characters, then check the inside.
Implement is_palindrome_recursive(text).
Input
text: The text to check (ASCII characters).
Output
Return true for a palindrome, otherwise false.
Contraintes
0 ≤ length ≤ 1,000
Exemples
Entrée :
text = "Never odd or even"
Sortie attendue :
expected = True
Entrée :
std::string text = "Never odd or even";
Sortie attendue :
bool expected = true;
Entrée :
string text = "Never odd or even";
Sortie attendue :
bool expected = true;
Explication
Letters only, lowercased: "neveroddoreven" reads the same both ways.
Entrée :
text = "code"
Sortie attendue :
expected = False
Entrée :
std::string text = "code";
Sortie attendue :
bool expected = false;
Entrée :
string text = "code";
Sortie attendue :
bool expected = false;
Explication
"c" and "e" differ, so it is not a palindrome.
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Skip non-letter and non-digit characters before comparing the ends.
Indice 2
Compare the outer characters first, then recurse on the remaining middle part.
Indice 3
Handle empty and single-character cleaned substrings as palindromes.
Complexité attendue
O(n) time, O(n) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.