Torna alle sfide
Facile 0 risolte 0% di accettazione

Palindrome String

Check whether text reads the same forwards and backwards.

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

0 ≤ length ≤ 10,000

Esempi

Esempio 1
Input:
text = "Racecar"

Output atteso:
expected = True
Input:
std::string text = "Racecar";

Output atteso:
bool expected = true;
Input:
string text = "Racecar";

Output atteso:
bool expected = true;

Spiegazione

Lowercased, "racecar" reads the same both ways.

Esempio 2
Input:
text = "hello"

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

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

Output atteso:
bool expected = false;

Spiegazione

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

Come affrontarlo

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

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

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.