Le migliori 150 intervisteMedio

Sottostringhe palindromiche

Guida dettagliata e implementazione Python per il problema delle "sottostringhe palindromiche".

Dichiarazione del problema

Medio

Data una stringa s, restituisce il numero di sottostringhe palindromiche in essa contenute.

Una stringa è palindroma quando si legge allo stesso modo sia all'indietro che in avanti. Una sottostringa è una sequenza contigua di caratteri all'interno della stringa.

Scrivi una funzione countSubstrings(s: str) -> int.

Vincoli
  • 1 <= len(s) <= 1000
  • s consists of lowercase English letters

Esempi

Example 1
Input
s = "abc"
Output
3
Explanation

Three palindromic substrings: "a", "b", "c".

Example 2
Input
s = "aaa"
Output
6
Explanation

Six palindromic substrings: "a", "a", "a", "aa", "aa", "aaa".

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per DP 1D 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.