From 93d89ea77f0d5a362ef5745da662e8531117a6f9 Mon Sep 17 00:00:00 2001 From: Uehara <134291500+X-XsleepZzz@users.noreply.github.com> Date: Sun, 22 Feb 2026 14:57:40 +0900 Subject: [PATCH 1/4] Add problem description for Group Anagrams Added problem statement and examples for grouping anagrams. --- 49. Group Anagrams.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 49. Group Anagrams.md diff --git a/49. Group Anagrams.md b/49. Group Anagrams.md new file mode 100644 index 0000000..b3a3c17 --- /dev/null +++ b/49. Group Anagrams.md @@ -0,0 +1,34 @@ +Given an array of strings strs, group the anagrams together. You can return the answer in any order. + + + +Example 1: + +Input: strs = ["eat","tea","tan","ate","nat","bat"] + +Output: [["bat"],["nat","tan"],["ate","eat","tea"]] + +Explanation: + +There is no string in strs that can be rearranged to form "bat". +The strings "nat" and "tan" are anagrams as they can be rearranged to form each other. +The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other. +Example 2: + +Input: strs = [""] + +Output: [[""]] + +Example 3: + +Input: strs = ["a"] + +Output: [["a"]] + + + +Constraints: + +1 <= strs.length <= 104 +0 <= strs[i].length <= 100 +strs[i] consists of lowercase English letters. From 59ac014479afbec33aab220bb90dfb5b39eeebd7 Mon Sep 17 00:00:00 2001 From: Uehara <134291500+X-XsleepZzz@users.noreply.github.com> Date: Sun, 22 Feb 2026 17:07:38 +0900 Subject: [PATCH 2/4] Refactor Group Anagrams problem statement and solution Updated the problem statement and example outputs for clarity. Refactored the solution code to use defaultdict for better handling of anagrams. --- 49. Group Anagrams.md | 99 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 20 deletions(-) diff --git a/49. Group Anagrams.md b/49. Group Anagrams.md index b3a3c17..b705250 100644 --- a/49. Group Anagrams.md +++ b/49. Group Anagrams.md @@ -1,34 +1,93 @@ -Given an array of strings strs, group the anagrams together. You can return the answer in any order. +49. Group Anagrams +Given an array of strings strs, group the anagrams together. You can return the answer in any order. - +Example 1: -Example 1: +Input: strs = ["eat","tea","tan","ate","nat","bat"] -Input: strs = ["eat","tea","tan","ate","nat","bat"] +Output: [["bat"],["nat","tan"],["ate","eat","tea"]] -Output: [["bat"],["nat","tan"],["ate","eat","tea"]] +Explanation: -Explanation: +There is no string in strs that can be rearranged to form "bat". +The strings "nat" and "tan" are anagrams as they can be rearranged to form each other. +The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other. +Example 2: -There is no string in strs that can be rearranged to form "bat". -The strings "nat" and "tan" are anagrams as they can be rearranged to form each other. -The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other. -Example 2: +Input: strs = [""] -Input: strs = [""] +Output: [[""]] -Output: [[""]] +Example 3: -Example 3: +Input: strs = ["a"] -Input: strs = ["a"] +Output: [["a"]] -Output: [["a"]] +Constraints: - +1 <= strs.length <= 10^4 +0 <= strs[i].length <= 100 +strs[i] consists of lowercase English letters. +(https://leetcode.com/problems/group-anagrams/description/) +--- +step1 +```py +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + key_to_char = {} + + for char in strs: + key = "".join(sorted(char)) -Constraints: + if key not in key_to_char: + key_to_char[key] = [] + key_to_char[key].append(char) + return list(key_to_char.values()) +``` +strs.lengthをnとして、strs[i].length、つまり文字列の長さをkとする。 +時間計算量: for文をstrs.length回、回すのでO(n)、中でsortedするのにO(klogk)、joinで文字列に整えて出すのにO(k)、つまり全体でO(n*klogk) +空間計算量: 辞書でkeyとしてsortした文字列を入れているのでO(k)、valueにはもとの文字列を入れるのでO(k)、よって内部ではO(k)。for文をstrs.length回、回すのでO(k*n) + +時間計算量は、最悪10^4 * 2*10^2 * log10 で大体10^6 < 10^7なのでセーフ +key_to_charって名前で良いのか少し気になる。 + +--- +(https://github.com/komdoroid/arai60/tree/14ec1184d68b0514455e688bdfc2ef39e1d787d8/HashMap/49.GroupAnagrams) +を読んだ。 + +defaultdictだとキーがないときに、勝手に穴埋めしてくれるので、こっちを使ったほうが良さそう。 +リストを引数に入れたら、キーがないとき[]をvalueに入れてくれる。 +辞書の変数名はかなり長くなるけど、"sorted_str_to_words"にする。 + +step2 +```py +import collections +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_str_to_words = collections.defaultdict(list) + + for original_str in strs: + sorted_str = "".join(sorted(original_str)) + sorted_str_to_words[sorted_str].append(original_str) + return list(sorted_str_to_words.values()) +``` + +変数名を少し考えてつけてみたけど、長すぎて逆に可読性落ちていないか心配。 +また、タイポしそう。 + +step3 3回連続pass +```py +import collections +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_str_to_words = collections.defaultdict(list) + + for original_str in strs: + sorted_str = "".join(sorted(original_str)) + sorted_str_to_words[sorted_str].append(original_str) + return list(sorted_str_to_words.values()) +``` +見直しもしたが、意外とタイポなしでゼロから何回も書けた。 +変数名に意味があるから、書いていてコードの意味がより理解しやすくなった気がする。 -1 <= strs.length <= 104 -0 <= strs[i].length <= 100 -strs[i] consists of lowercase English letters. From e59fda2225be76f4687b0ad74537b44f45498520 Mon Sep 17 00:00:00 2001 From: Uehara <134291500+X-XsleepZzz@users.noreply.github.com> Date: Sun, 22 Feb 2026 17:08:47 +0900 Subject: [PATCH 3/4] Fix formatting in Group Anagrams constraints Remove unnecessary line break in constraints section. --- 49. Group Anagrams.md | 1 - 1 file changed, 1 deletion(-) diff --git a/49. Group Anagrams.md b/49. Group Anagrams.md index b705250..65d1368 100644 --- a/49. Group Anagrams.md +++ b/49. Group Anagrams.md @@ -25,7 +25,6 @@ Input: strs = ["a"] Output: [["a"]] Constraints: - 1 <= strs.length <= 10^4 0 <= strs[i].length <= 100 strs[i] consists of lowercase English letters. From 96790788a9a51753cf86c815816d023891073b3a Mon Sep 17 00:00:00 2001 From: Uehara <134291500+X-XsleepZzz@users.noreply.github.com> Date: Sun, 22 Feb 2026 17:09:18 +0900 Subject: [PATCH 4/4] Fix formatting and add step1 section in markdown --- 49. Group Anagrams.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/49. Group Anagrams.md b/49. Group Anagrams.md index 65d1368..ebfab7f 100644 --- a/49. Group Anagrams.md +++ b/49. Group Anagrams.md @@ -28,7 +28,8 @@ Constraints: 1 <= strs.length <= 10^4 0 <= strs[i].length <= 100 strs[i] consists of lowercase English letters. -(https://leetcode.com/problems/group-anagrams/description/) +(https://leetcode.com/problems/group-anagrams/description/) + --- step1 ```py