Le migliori 150 intervisteFacile

Palindromo valido

Guida dettagliata e implementazione Python per il problema del "palindromo valido".

Dichiarazione del problema

Facile

Una frase è palindroma se, dopo aver convertito tutte le lettere maiuscole in minuscole e aver rimosso tutti i caratteri non alfanumerici, si legge allo stesso modo sia in avanti che all'indietro. I caratteri alfanumerici includono lettere e numeri.

Data una stringa s, restituisce True se è palindromo, o False altrimenti.

Scrivi una funzione isPalindrome(s: str) -> bool.

Vincoli
  • 1 <= len(s) <= 2 * 10^5
  • s consists only of printable ASCII characters

Esempi

Example 1
Input
s = "A man, a plan, a canal: Panama"
Output
True
Explanation

After removing non-alphanumeric characters and converting to lowercase: "amanaplanacanalpanama", which is a palindrome.

Example 2
Input
s = "race a car"
Output
False
Explanation

After processing: "raceacar" is not a palindrome.

Example 3
Input
s = " "
Output
True
Explanation

After removing non-alphanumeric characters, s is an empty string. An empty string is a palindrome by definition.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Two Pointers come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.