Le migliori 150 intervisteFacile

Anagrammi di gruppo

Guida dettagliata e implementazione Python per il problema degli "Anagrammi di gruppo".

Dichiarazione del problema

Facile

Dato un array di stringhe strs, raggruppa insieme gli anagrammi. Puoi restituire la risposta in qualsiasi ordine.

Un anagramma è una parola o una frase formata riorganizzando le lettere di una parola o frase diversa, utilizzando tutte le lettere originali esattamente una volta.

Scrivi una funzione groupAnagrams(strs: List[str]) -> List[List[str]].

Vincoli
  • 1 <= len(strs) <= 10^4
  • 0 <= len(strs[i]) <= 100
  • strs[i] consists of lowercase English letters

Esempi

Example 1
Input
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output
[["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
Explanation

"eat", "tea", and "ate" are anagrams of each other. "tan" and "nat" are anagrams. "bat" has no anagram in the list.

Example 2
Input
strs = [""]
Output
[[""]]
Explanation

A single empty string forms its own group.

Example 3
Input
strs = ["a"]
Output
[["a"]]
Explanation

A single character string forms its own group.

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