Python Nozioni di baseFacile

Problema della parentesi bilanciata

Guida dettagliata e implementazione Python per il problema "Problema delle parentesi bilanciate".

Dichiarazione del problema

Facile

Scrivi una funzione is_balanced(s) che accetta una stringa s contenente solo i caratteri '(', ')', '{', '}', '[' e ']' e restituisce True se le parentesi sono bilanciate e annidate correttamente, False altrimenti. Una stringa vuota è considerata bilanciata.

Vincoli
  • 0 <= len(s) <= 10^4
  • s consists only of '(', ')', '{', '}', '[', ']'

Esempi

Example 1
Input
s = '({[]})'
Output
True
Explanation

Each opening bracket has a matching closing bracket in the correct order: ( matches ), { matches }, [ matches ].

Example 2
Input
s = '({[})'
Output
False
Explanation

The [ is closed by } which is a mismatch.

Example 3
Input
s = ''
Output
True
Explanation

An empty string is considered balanced.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche degli array 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.