Tutte le sfide
Media 0 risolte 0% di accettazione

Recursive Palindrome

Check a palindrome recursively, ignoring case and punctuation.

Cosa imparerai

  • Verificare un palindromo con la ricorsione
  • Ignorare maiuscole e punteggiatura
  • Ridurre il problema dalle due estremità

Problema

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

Salta i caratteri non lettera e non cifra prima di confrontare le estremità.

Suggerimento 2

Confronta prima i caratteri esterni, poi richiama ricorsivamente la parte centrale restante.

Suggerimento 3

Gestisci le sottostringhe pulite vuote e di un solo carattere come palindrome.

Complessità attesa

tempo O(n), spazio aggiuntivo O(n)

Riferimenti

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

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.