Tutte le sfide
Media 3 risolte 100.0% di accettazione

Recursive Palindrome

Check a palindrome recursively, ignoring case and punctuation.

Cosa imparerai

  • Check palindromes recursively
  • Ignore case and punctuation
  • Shrink the problem from both ends

Problema

Mostra traduzione

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.

Vincoli

0 ≤ length ≤ 1,000

Esempi

Esempio 1
Input:
text = "Never odd or even"

Output atteso:
expected = True
Input:
std::string text = "Never odd or even";

Output atteso:
bool expected = true;
Input:
string text = "Never odd or even";

Output atteso:
bool expected = true;

Spiegazione

Letters only, lowercased: "neveroddoreven" reads the same both ways.

Esempio 2
Input:
text = "code"

Output atteso:
expected = False
Input:
std::string text = "code";

Output atteso:
bool expected = false;
Input:
string text = "code";

Output atteso:
bool expected = false;

Spiegazione

"c" and "e" differ, so it is not a palindrome.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Skip non-letter and non-digit characters before comparing the ends.

Suggerimento 2

Compare the outer characters first, then recurse on the remaining middle part.

Suggerimento 3

Handle empty and single-character cleaned substrings as palindromes.

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 LMLaura Mahlow Python 962 ms - 4/5 Vedi il codice
2 RCRiccardo Corti Python 965 ms - 4/5 Vedi il codice
3 Н"Никита "Programmer" Python 983 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 3 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.