150 najlepszych wywiadówŁatwe

Anagramy grupowe

Szczegółowy przewodnik i implementacja Python dla problemu „Anagramy grupowe”.

Oświadczenie o problemie

Łatwe

Mając tablicę ciągów strs, zgrupuj anagramy razem. Odpowiedź możesz zwrócić w dowolnej kolejności.

Anagram to słowo lub fraza utworzona przez zmianę układu liter innego słowa lub frazy, przy użyciu wszystkich oryginalnych liter dokładnie raz.

Napisz funkcję groupAnagrams(strs: List[str]) -> List[List[str]].

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla tablic i haszowania, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.