From 5027c196497faca63234aa548cb31e9326dec165 Mon Sep 17 00:00:00 2001 From: Chanseok Lim Date: Wed, 30 Apr 2025 20:05:23 +0900 Subject: [PATCH 1/3] Solved 49. Grouped Anagrams --- 49/49 | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 49/49 diff --git a/49/49 b/49/49 new file mode 100644 index 0000000..46f584f --- /dev/null +++ b/49/49 @@ -0,0 +1,90 @@ +## Step1 +5分以内に書けなかった。 +各単語に対して、{a_i: n_i}, a_i = i番目のアルファベット、n_i = その単語がi番目のアルファベットを含む数というdictを作成して、dict同士を比べれば何とかなるかなと思い始めて頃に5分が経っていた。 +よく考えるとこの実装はアルファベット以外のstrはどうするんだという問題がある。 +sortして同一であるのがanagramだとは気づかなかったので、解答を見てなるほどと思った。 +```python +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + d = defaultdict(list) + for s in strs: + k = ''.join(sorted(s)) + d[k].append(s) + + return list(d.values()) +``` +## Step2 +レビュー依頼の直近5人についてレビューを読む。 +[https://github.com/shintaro1993/arai60/pull/16](https://github.com/shintaro1993/arai60/pull/16)
+sortしないで済む場合分けを入れてる。

+ +[https://github.com/plushn/SWE-Arai60/pull/12](https://github.com/plushn/SWE-Arai60/pull/12)
+小文字以外は弾く。そういえば小文字大文字はどうsortされるんだったか?
+問題の設定は全てlowercaseだけど、"Ab"と"ab"は同じとして扱うべきか? それとも弾くべきか?

+ +[https://github.com/fuga-98/arai60/pull/13](https://github.com/fuga-98/arai60/pull/13)
+```python +my_list = ['a', 'b', 'c'] +print(str(my_list)) +# 出力: "['a', 'b', 'c']" +``` +strにlist入れるとそうなるのか。

+ +[https://github.com/ichika0615/arai60/pull/11](https://github.com/ichika0615/arai60/pull/11)
+単語をsortした後にordにして、'a'との差分からアルファベットを特定する。

+ +[https://github.com/Mahiro-3612/leetcode/pull/2](https://github.com/Mahiro-3612/leetcode/pull/2)
+時間計算量も求めること。
< +Cf. [https://discord.com/channels/1084280443945353267/1307605446538039337/1336992875577081917](https://discord.com/channels/1084280443945353267/1307605446538039337/1336992875577081917)
+ +```python +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + if not isinstance(strs, list): + raise TypeError(f'Expected: list, Actual: {type(strs)}') + + grouped_anagrams = defaultdict(list) + for s in strs: + basis_anagram = ''.join(sorted(s)) + grouped_anagrams[basis_anagram].append(s) + + return list(grouped_anagrams.values()) +``` +sortedの計算量はO(nlogn)らしい
+[https://stackoverflow.com/questions/14434490/what-is-the-complexity-of-the-sorted-function](https://stackoverflow.com/questions/14434490/what-is-the-complexity-of-the-sorted-function)
+すると +計算量:O(mnlogn), m, n = strs.length, max(strs[i].length) +時間計算量: 2*10^6 / 10^6 = 2 s + +## Step3 +10分に3回できた。 + +## 感想 +最初の方針と前回を思い出しながらコードを実装して動かすと`TypeError: unhashable type: 'collections.defaultdict'`となった。
+dictをkeyするのは流石に無茶かと思っていたが、やはり無茶だった。
+hashableについて2つの記事、 +[hashableについてのqiita記事](https://qiita.com/yoichi22/items/ebf6ab3c6de26ddcc09a#__hash__-%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%E3%81%8C%E3%81%82%E3%82%8C%E3%81%B0-hashable)と +[公式ドキュメントの__hash__の項目](https://docs.python.org/ja/3/reference/datamodel.html#object.__hash__)、 +を読む。面白い。考えたこともなかった。 +```python +class Solution: + def count_unicodes_in_word(self, word: str) -> dict: + # type check + if type(word) is not str: + raise TypeError(f'expected: str, actual: {type(word)}') + + unicode_to_frequency = defaultdict(int) + for s in word: + unicode_to_frequency[ord(s)] += 1 + + return unicode_to_frequency + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + + unicode_frequency_maps = defaultdict(list) + for s in strs: + uc_to_freq = self.count_unicodes_in_word(s) + unicode_frequency_maps[uc_to_freq].append(s) + + return list(unicode_frequency_maps.values()) +``` From 9a22a27d539fff0de637a81989368dcb1a546ea2 Mon Sep 17 00:00:00 2001 From: Chanseok Lim Date: Wed, 30 Apr 2025 20:08:20 +0900 Subject: [PATCH 2/3] Rename 49 to 49.md --- 49/{49 => 49.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 49/{49 => 49.md} (100%) diff --git a/49/49 b/49/49.md similarity index 100% rename from 49/49 rename to 49/49.md From f227d2f3e1a88c7351f1824d90bdf2dfa772377f Mon Sep 17 00:00:00 2001 From: Chanseok Lim Date: Wed, 30 Apr 2025 22:32:17 +0900 Subject: [PATCH 3/3] Update 49.md --- 49/49.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/49/49.md b/49/49.md index 46f584f..190b2fd 100644 --- a/49/49.md +++ b/49/49.md @@ -88,3 +88,13 @@ class Solution: return list(unicode_frequency_maps.values()) ``` +Step2で書いたコードの +``` +if not isinstance(strs, list): + raise TypeError(f'Expected: list, Actual: {type(strs)}') +``` +は +``` +assert isinstance(strs, list), f'Expected: list, Actual: {type(strs)}' +``` +の方がいいかも。