150強訪談簡單

組字謎

“Group Anagrams”問題的詳細指南和 Python 實作。

問題陳述

簡單

給定一個字串陣列 strs,將字謎組合在一起。您可以按任意順序返回答案。

字謎詞是透過使用所有原始字母一次重新排列不同單字或短語的字母而形成的單字或短語。

寫一個函數 groupAnagrams(strs: List[str]) -> List[List[str]]

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

範例

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?
考慮使用數組和哈希特定的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

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

在編輯器中開啟
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

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。