LeetCode-49.字母異位詞分組(Group Anagrams)

2021-02-21 極客算法

49. 字母異位詞分組

給定一個字符串數組,將字母異位詞組合在一起。字母異位詞指字母相同,但排列不同的字符串。

示例:

輸入: ["eat", "tea", "tan", "ate", "nat", "bat"]輸出:[  ["ate","eat","tea"],  ["nat","tan"],  ["bat"]]

說明:

所有輸入均為小寫字母。

不考慮答案輸出的順序。

來源:力扣(LeetCode)

連結:https://leetcode-cn.com/problems/group-anagrams/

Link:https://leetcode.com/problems/group-anagrams/

排序+哈希

O(N * KlogK), N等於單詞個數,K等於單詞平均長度

class Solution:    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
seen = {} res = []
for word in strs: sort_word = ''.join(sorted(word)) if sort_word in seen: index = seen[sort_word] res[index].append(word) else: ans = [word] index = len(res) res.append(ans) seen[sort_word] = index
return res

哈希

O(N * K), 理論上更快一些,python代碼實際並不是

將字符串用數組表示, 例如: "eat"

[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z] 1           1                                            1 

代碼如下:

class Solution:    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
seen = {} res = [] base = ord('a')
for word in strs: word_count = [0 for i in range(26)] for char in word: index = ord(char) - base word_count[index] += 1 word_count = tuple(word_count)
if word_count in seen: index = seen[word_count] res[index].append(word) else: ans = [word] index = len(res) res.append(ans) seen[word_count] = index
return res

--End--

相關焦點

  • LeetCode 49. 字母異位詞分組 | Python
    49.
  • leetcode每日一題:49.字母異位詞分組
    leetcode每日一題:49.字母異位詞分組:https://leetcode-cn.com/problems/group-anagrams/一起刷題吧一、題意分析 輸入:由字符串組成的列表(數組)輸出:將由同樣的字母和字母個數組成的不同序列放在一個列表裡,然後整體做為一個列表輸出難度
  • 【LeetCode】49. 字母異位詞分組
    【LeetCode】49.
  • leetcode每日一題:49. 字母異位詞分組
    題目:https://leetcode-cn.com/problems/group-anagrams給定一個字符串數組,將字母異位詞組合在一起
  • 49. 字母異位詞分組
    49.
  • ​LeetCode刷題實戰49:字母異位詞分組
    今天和大家聊的問題叫做 字母異位詞分組,我們先來看題面:https://leetcode-cn.com/problems/group-anagrams/Given an array of strings strs, group the anagrams together.
  • LeetCode Top 100 高頻算法題 49. Group Anagrams
    LeetCode算法題有一個技巧:先看每道題的example,大部分情況看完example就能理解題目意思;如果看完example還有疑惑,可以再回過頭來看英文題目,這樣也可以節省一點時間~Given an array of strings strs, group the anagrams
  • 242 有效的字母異位詞
    題目地址:https://leetcode-cn.com
  • leetcode算法之哈希表
    今天該來盤一盤 哈希表 這類題目分類別解析leetcode上的一些相關的例題路,代碼採用C++與python實現。
  • Go 實現 LeetCode 全集
    Water - https://leetcode.com/problems/container-with-most-water/Binary[X] Sum of Two Integers - https://leetcode.com/problems/sum-of-two-integers/[X] Number of 1 Bits - https://leetcode.com
  • sql 分組語句group by
    聚合函數如sum(),max()等,通常需要結合分組語句group by 使用GROUP BY 語句用於結合合計函數,根據一個或多個列對結果集進行分組
  • Oracle分組查詢group by的用法及講解
    group by是sql中比較強大的功能,是在對數據分組統計時必不可少的用法。但是,對於很多經驗不足的同學,經常會寫錯。今天我們就以Oracle為例,來講解下分組查詢group by的用法。原因是group by 分組查詢,select子句後的欄位必須來自group by後的分組欄位。於是 我們執行SQLSELECT SSEX FROM STUDENT GROUP BY SSEX;這下成功地將數據分為了兩組。
  • Mysql分組查詢group by使用示例
    (1) group by的含義:將查詢結果按照1個或多個欄位進行分組,欄位值相同的為一組(2) group by可用於單個欄位分組
  • 聊聊mysql分組查詢group by以及分組條件having的用法
    今天和大家一起學習一下mysql的分組查詢group by的使用方法,也是重新回憶和複習一下。我們來看一下分組查詢的語法:1、語法:group by + 分組的欄位;下面我們來看一張學生表信息:我們現在需要實現這樣一個需求:1、按性別分組,分別查詢出男、女學生的數學平均分
  • LeetCode-25.K個一組翻轉鍊表(Reverse Nodes in k-Group)
    來源:力扣(LeetCode)連結:https://leetcode-cn.com/problems/reverse-nodes-in-k-group/Link:https://leetcode.com/problems/reverse-nodes-in-k-group/模擬法當k == 0或者當
  • 分組?原來你是玩兒真的!
    數據計算中,分組絕對是最常用的計算方式之一,相應的,在SQL語言中,自然會用到group by了。但是,不知道你想過沒有,SQL中的group by並不能算是「純粹」的分組,它實際上並不是針對原始數據分組,而是將結果集分組,最終是為了實現5類聚合計算:min/max/avg/count/sum,而單獨使用group by沒有任何意義,只是相當於按照group by的條件進行了排序而已。但實際的數據計算中,針對分組數據的統計要求有可能遠遠超過5類聚合計算的能力。
  • 【leetcode】 242. Valid Anagram
    有效的字母異位詞Given two strings s and t , write a function to determine if t is an anagram of s.給定兩個字符串 s 和 t ,編寫一個函數來判斷 t 是否是 s 的字母異位詞。
  • NET開發-在C#的LINQ查詢語句中,如何使用group……by……into分組
    在此代碼中,是按Sex欄位的值進行分組,使用s.Key表示分組的鍵,可以循環輸出Sex的值。案例一:使用Group…by分組輸出對C#泛型列表List<Student>集合中的Sex進行分組,並輸出分組欄位Sex的值,然後再輸出每個分組結果,C#代碼如下:List<Student> StudentList1 = new List<Student&