Le migliori 150 intervisteFacile

Genera parentesi

Guida dettagliata e implementazione Python per il problema 'Genera parentesi'.

Dichiarazione del problema

Facile

Date le n coppie di parentesi, scrivi una funzione per generare tutte le combinazioni di parentesi ben formate.

Scrivi una funzione generateParenthesis(n: int) -> List[str].

Vincoli
  • 1 <= n <= 8

Esempi

Example 1
Input
n = 3
Output
["((()))", "(()())", "(())()", "()(())", "()()()"]
Explanation

All 5 valid combinations of 3 pairs of parentheses.

Example 2
Input
n = 1
Output
["()"]
Explanation

Only one valid combination with 1 pair.

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