From f99e93f594b7fe3325f4f87985f897b7da791944 Mon Sep 17 00:00:00 2001 From: Kazuki Kitano Date: Sun, 1 Mar 2026 18:46:48 +0900 Subject: [PATCH 1/6] step1 --- memo.md | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++ step1-1.py | 14 +++++ step1-2.py | 44 +++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 step1-1.py create mode 100644 step1-2.py diff --git a/memo.md b/memo.md index 4bd0397..65441c5 100644 --- a/memo.md +++ b/memo.md @@ -1 +1,162 @@ # Step1 + +## アプローチ + +* 文字列に出現する文字の種類と回数が一緒のものをグループにしたい +* それぞれの文字の種類ごとにハッシュを作る?例えば`ord()`使ったりして. +* でもそれだと, 違う文字の組み合わせだけど, 同じハッシュになるものが存在する. +* それぞれの文字列をソートしたら, 同じ文字の出現回数と種類にものは同じ文字列に行き着く. +* 感覚的にはあまり効率的じゃなさそうだから一回見積もり +* 文字列をソート +* それを辞書かなんかに保存. (あるいはUnion Find? =>あとで検討) +* 辞書には, ソート後の文字列をキー, 元の文字列の配列を値として保存 +* 各文字列(長さm)ごとの処理は, ソートの`O(mlog(m))`と辞書のルックアップの`O(1)`, 該当した配列にappendの`O(1)`. +* それを全部の文字列やる `O(n)` +* 最後に, 辞書から答えを持ってくる + * これはグループの数だけ, 結果を格納する配列に`append`する. + * グループの数は最悪の場合で`n`個 +* トータル + * `O(n * m * log(m) + n)`だから`O(n * m * log(m))` +* 検討事項 + * 辞書の代わりに`Union find`を使えるか検討する + * これより効率的なアルゴリズムがないかもう少し考える +* `Union find`についての検討 + * 二つのノード間(要素間)のエッジの有無をもとにグループ分けできる. + * 全体のグラフから, 森を抽出するみたいなイメージ + * 今回はそれぞれが繋がっているか見るために, 全ての組み合わせを見なければいけない?? + * これには`O(n ^ 2)`かかる + * この時点で`Union find`は使えなさそう +* 他のアルゴリズムの可能性を検討 + * 無駄がありそうなのは, ソートしている部分. ソートしなくてもグループ分けに使えるkeyを作れないかな. + * いい感じにハッシュを作る方法はあるか + * `O(m)`で各文字列ごとに, そこに含まれる文字の種類と個数がわかったら嬉しい. + * 例えば, `m`は最大でも`100`だから`2^7 - 1`あればいい. `7 bit unsigned`で表現可能 + * 各アルファベットごとに`7bit`を用意. それらを並べて`7 * 26 bit`にすれば, 各アルファベットの種類ごとの個数をまとめたものを一意の数字で表せる + * でも`7 * 26 bit`って`182 bit`だし, それが, `10^4`個あるとなると, `10^4 * 128 bit`のスペースを使用する. + * `182 bit`ってどんくらいかな + * 一つの文字を表すのが, `4 byte`で`32 bit`かな. + * あれ, `unicode`も`4byte`だったっけ??自信はない + * `182 bit`は文字で言うと6文字分くらい + * `6 * 10^4 = 60000`文字程度なら普通のコード書いていて読み書きすることも多い + * じゃあ特段気にしなければいけないほど空間を圧迫しているわけでもなさそう + * このハッシュ生成方法で問題を解いた場合の計算量 + * ハッシュの計算に`O(m)`で, それ以外は`sort`の方法と一緒 + * 全体だと, `O(n * m)`か. + * ただ, この方法はアルファベットが増えたり, 大文字も許容するようになると増えた文字数分空間を圧迫することになる + * このハッシュを作るためには, その文字列に含まれる文字ごとの個数を記録する + * ってことは, わざわざbitによる一つの数字にしなくても同じようなことができる??? + * 二次元配列を用意するみたいな + * `something[アルファベットが何か][そのアルファベットの個数][アルファベットが何か][そのアルファベットの個数]...` + * 何次元配列だ??? + * アルファベットの種類 = 26 + * アルファベットの個数の最大 = 100 + * 2600次元配列?? + * 配列に格納するのは, そのグループを表すはっしゅ?? + * これは`182 bit`使う方法よりも空間を圧迫しそう +* 今回は二つの方法で実装する + * ソートを使う方法 (pattern 1) + * ハッシュを使う方法 (pattern 2) + + +## Code1-2 + +### 以下は120件目のテストケースでエラーしたコード + +```python +from typing import List + + +class Solution: + def calculate_hash(self, word): + + # Calculate value representing (alphabet, count) pair. + def calculate_value(alphabet, count): + seven_bit_maximum = 0b1111111 + if count < 0 or count > seven_bit_maximum: + raise ValueError(f"count: {count} must be zero or positive and less than or equal to {seven_bit_maximum}") + + a_ord = ord("a") + z_ord = ord("z") + alphabet_ord = ord(alphabet) + if alphabet_ord < a_ord or z_ord < alphabet_ord: + raise ValueError(f"alphabet: {alphabet} must be small english letter") + + position = alphabet_ord - a_ord + return count << position + + + chr_to_count = {} + for chr in word: + chr_to_count.setdefault(chr, 0) + chr_to_count[chr] += 1 + + hash = 0 + for chr, count in chr_to_count.items(): + hash += calculate_value(chr, count) + return hash + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + hash_value = self.calculate_hash(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) + +``` + +* `bd`と`aacc`が同じグループに割り当てられている以外はあっていた. +* なぜこれらが同じハッシュを持ったのか?? + * オーバーフロー??って思ったけど, 起こり得るのは`z`がいっぱい存在している時なはず +* とりあえずそれぞれのハッシュ値を`print`してみる + * `bd`も`aacc`も`10`だった. + * それ以外のものも全体的に想定していたより`hash`の値が小さすぎる + * `<< position`ダメだ. `7bit`ずつ動かせていない + +### ACとなったコード + +```python +from typing import List + + +class Solution: + def calculate_hash(self, word): + + # Calculate value representing (alphabet, count) pair. + def calculate_value(alphabet, count): + seven_bit_maximum = 0b1111111 + if count < 0 or count > seven_bit_maximum: + raise ValueError(f"count: {count} must be zero or positive and less than or equal to {seven_bit_maximum}") + + a_ord = ord("a") + z_ord = ord("z") + alphabet_ord = ord(alphabet) + if alphabet_ord < a_ord or z_ord < alphabet_ord: + raise ValueError(f"alphabet: {alphabet} must be small english letter") + + position = alphabet_ord - a_ord + num_bit_for_each = 7 + return count << position * num_bit_for_each + + + chr_to_count = {} + for chr in word: + chr_to_count.setdefault(chr, 0) + chr_to_count[chr] += 1 + + hash = 0 + for chr, count in chr_to_count.items(): + hash += calculate_value(chr, count) + return hash + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + hash_value = self.calculate_hash(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) + +``` \ No newline at end of file diff --git a/step1-1.py b/step1-1.py new file mode 100644 index 0000000..027ee9c --- /dev/null +++ b/step1-1.py @@ -0,0 +1,14 @@ +from typing import List + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_word_to_group = {} + for word in strs: + sorted_word = "".join(sorted(word)) + sorted_word_to_group.setdefault(sorted_word, []) + sorted_word_to_group[sorted_word].append(word) + result = [] + for group in sorted_word_to_group.values(): + result.append(group) + return result + \ No newline at end of file diff --git a/step1-2.py b/step1-2.py new file mode 100644 index 0000000..d9e022c --- /dev/null +++ b/step1-2.py @@ -0,0 +1,44 @@ +from typing import List + + +class Solution: + def calculate_hash(self, word): + + # Calculate value representing (alphabet, count) pair. + def calculate_value(alphabet, count): + seven_bit_maximum = 0b1111111 + if count < 0 or count > seven_bit_maximum: + raise ValueError(f"count: {count} must be zero or positive and less than or equal to {seven_bit_maximum}") + + a_ord = ord("a") + z_ord = ord("z") + alphabet_ord = ord(alphabet) + if alphabet_ord < a_ord or z_ord < alphabet_ord: + raise ValueError(f"alphabet: {alphabet} must be small english letter") + + position = alphabet_ord - a_ord + num_bit_for_each = 7 + return count << position * num_bit_for_each + + + chr_to_count = {} + for chr in word: + chr_to_count.setdefault(chr, 0) + chr_to_count[chr] += 1 + + hash = 0 + for chr, count in chr_to_count.items(): + hash += calculate_value(chr, count) + return hash + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + hash_value = self.calculate_hash(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) + + + \ No newline at end of file From ebf936fc3e086a75086b2b5a290611c25307d5e2 Mon Sep 17 00:00:00 2001 From: Kazuki Kitano Date: Sun, 1 Mar 2026 18:58:45 +0900 Subject: [PATCH 2/6] step2 --- memo.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ step2-1.py | 11 +++++++++ step2-2.py | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 step2-1.py create mode 100644 step2-2.py diff --git a/memo.md b/memo.md index 65441c5..29ea24c 100644 --- a/memo.md +++ b/memo.md @@ -159,4 +159,71 @@ class Solution: hash_to_group[hash_value].append(word) return list(hash_to_group.values()) +``` + +# Step2 + +## Code2-1 + +* `sorted_word_to_group.values()`で得られるものを直接`return`するように変更 + +```python +from typing import List + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_word_to_group = {} + for word in strs: + sorted_word = "".join(sorted(word)) + sorted_word_to_group.setdefault(sorted_word, []) + sorted_word_to_group[sorted_word].append(word) + return list(sorted_word_to_group.values()) +``` + +## Code2-2 + +* `chr`や`hash`などのライブラリに含まれる関数を避けた. +* 変数名や関数名を変更 + +```python +from typing import List + + +class Solution: + def calculate_hash_by_alphabet_count(self, word): + + def calculate_alphabet_count_value(alphabet, count): + bit_span = 7 + bit_maximum = 2 ** bit_span - 1 + if count < 0 or count > bit_maximum: + raise ValueError(f"count: {count} must be zero or positive and less than or equal to {bit_maximum}") + + a_ord = ord("a") + z_ord = ord("z") + alphabet_ord = ord(alphabet) + if alphabet_ord < a_ord or z_ord < alphabet_ord: + raise ValueError(f"alphabet: {alphabet} must be small english letter") + + position = alphabet_ord - a_ord + return count << position * bit_span + + + alphabet_to_count = {} + for alphabet in word: + alphabet_to_count.setdefault(alphabet, 0) + alphabet_to_count[alphabet] += 1 + + hash = 0 + for alphabet, count in alphabet_to_count.items(): + hash += calculate_alphabet_count_value(alphabet, count) + return hash + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + hash_value = self.calculate_hash_by_alphabet_count(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) ``` \ No newline at end of file diff --git a/step2-1.py b/step2-1.py new file mode 100644 index 0000000..f6b89d8 --- /dev/null +++ b/step2-1.py @@ -0,0 +1,11 @@ +from typing import List + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_word_to_group = {} + for word in strs: + sorted_word = "".join(sorted(word)) + sorted_word_to_group.setdefault(sorted_word, []) + sorted_word_to_group[sorted_word].append(word) + return list(sorted_word_to_group.values()) + \ No newline at end of file diff --git a/step2-2.py b/step2-2.py new file mode 100644 index 0000000..8608beb --- /dev/null +++ b/step2-2.py @@ -0,0 +1,42 @@ +from typing import List + + +class Solution: + def calculate_hash_by_alphabet_count(self, word): + + def calculate_alphabet_count_value(alphabet, count): + bit_span = 7 + bit_maximum = 2 ** bit_span - 1 + if count < 0 or count > bit_maximum: + raise ValueError(f"count: {count} must be zero or positive and less than or equal to {bit_maximum}") + + a_ord = ord("a") + z_ord = ord("z") + alphabet_ord = ord(alphabet) + if alphabet_ord < a_ord or z_ord < alphabet_ord: + raise ValueError(f"alphabet: {alphabet} must be small english letter") + + position = alphabet_ord - a_ord + return count << position * bit_span + + + alphabet_to_count = {} + for alphabet in word: + alphabet_to_count.setdefault(alphabet, 0) + alphabet_to_count[alphabet] += 1 + + hash = 0 + for alphabet, count in alphabet_to_count.items(): + hash += calculate_alphabet_count_value(alphabet, count) + return hash + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + hash_value = self.calculate_hash_by_alphabet_count(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) + + \ No newline at end of file From c63e6b4102b922b397754bf8f597b05db2c0650e Mon Sep 17 00:00:00 2001 From: Kazuki Kitano Date: Sun, 1 Mar 2026 19:10:21 +0900 Subject: [PATCH 3/6] =?UTF-8?q?step3=201=E5=9B=9E=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- step3-1.py | 12 ++++++++++++ step3-2.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 step3-1.py create mode 100644 step3-2.py diff --git a/step3-1.py b/step3-1.py new file mode 100644 index 0000000..aabc6e2 --- /dev/null +++ b/step3-1.py @@ -0,0 +1,12 @@ +from typing import List + + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_word_to_group = {} + for word in strs: + sorted_word = "".join(sorted(word)) + sorted_word_to_group.setdefault(sorted_word, []) + sorted_word_to_group[sorted_word].append(word) + return list(sorted_word_to_group.values()) + diff --git a/step3-2.py b/step3-2.py new file mode 100644 index 0000000..1978fc1 --- /dev/null +++ b/step3-2.py @@ -0,0 +1,30 @@ +from typing import List + + +class Solution: + def calc_hash_by_alphabet_count(self, word, bit_span=7): + alphabet_to_count ={} + for alphabet in word: + alphabet_to_count.setdefault(alphabet, 0) + alphabet_to_count[alphabet] += 1 + + + hash_value = 0 + for alphabet, count in alphabet_to_count.items(): + if count > 2 ** bit_span - 1: + raise ValueError("bit span is too small") + + alphabet_count_value = count << (ord(alphabet) - ord("a")) * bit_span + hash_value = hash_value | alphabet_count_value + + return hash_value + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + word_hash = self.calc_hash_by_alphabet_count(word) + hash_to_group.setdefault(word_hash, []) + hash_to_group[word_hash].append(word) + return list(hash_to_group.values()) + From 4fa6961c2fca87c940674ddac127b82dbaa4e15e Mon Sep 17 00:00:00 2001 From: Kazuki Kitano Date: Sun, 1 Mar 2026 19:15:43 +0900 Subject: [PATCH 4/6] =?UTF-8?q?step3=203=E5=9B=9E=E3=82=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- step3-1.py | 9 ++++----- step3-2.py | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/step3-1.py b/step3-1.py index aabc6e2..c6028de 100644 --- a/step3-1.py +++ b/step3-1.py @@ -3,10 +3,9 @@ class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - sorted_word_to_group = {} + sorted_word_to_count = {} for word in strs: sorted_word = "".join(sorted(word)) - sorted_word_to_group.setdefault(sorted_word, []) - sorted_word_to_group[sorted_word].append(word) - return list(sorted_word_to_group.values()) - + sorted_word_to_count.setdefault(sorted_word, []) + sorted_word_to_count[sorted_word].append(word) + return list(sorted_word_to_count.values()) \ No newline at end of file diff --git a/step3-2.py b/step3-2.py index 1978fc1..980b2a9 100644 --- a/step3-2.py +++ b/step3-2.py @@ -3,28 +3,28 @@ class Solution: def calc_hash_by_alphabet_count(self, word, bit_span=7): - alphabet_to_count ={} + alphabet_to_count = {} for alphabet in word: alphabet_to_count.setdefault(alphabet, 0) alphabet_to_count[alphabet] += 1 - - + hash_value = 0 + maximum_representable = 2 ** bit_span - 1 for alphabet, count in alphabet_to_count.items(): - if count > 2 ** bit_span - 1: + if count > maximum_representable: raise ValueError("bit span is too small") - alphabet_count_value = count << (ord(alphabet) - ord("a")) * bit_span hash_value = hash_value | alphabet_count_value - + return hash_value + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: hash_to_group = {} for word in strs: - word_hash = self.calc_hash_by_alphabet_count(word) - hash_to_group.setdefault(word_hash, []) - hash_to_group[word_hash].append(word) - return list(hash_to_group.values()) - + hash_value = self.calc_hash_by_alphabet_count(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) \ No newline at end of file From 325b31771fb1e7d97ef0f169e58032d35be1c419 Mon Sep 17 00:00:00 2001 From: Kazuki Kitano Date: Sun, 1 Mar 2026 19:22:18 +0900 Subject: [PATCH 5/6] step3 md --- memo.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/memo.md b/memo.md index 29ea24c..cd08205 100644 --- a/memo.md +++ b/memo.md @@ -226,4 +226,59 @@ class Solution: hash_to_group.setdefault(hash_value, []) hash_to_group[hash_value].append(word) return list(hash_to_group.values()) +``` + +# Step3 + +## Code3-1 + +```python +from typing import List + + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + sorted_word_to_count = {} + for word in strs: + sorted_word = "".join(sorted(word)) + sorted_word_to_count.setdefault(sorted_word, []) + sorted_word_to_count[sorted_word].append(word) + return list(sorted_word_to_count.values()) + +``` + +## Code3-2 + +```python +from typing import List + + +class Solution: + def calc_hash_by_alphabet_count(self, word, bit_span=7): + alphabet_to_count = {} + for alphabet in word: + alphabet_to_count.setdefault(alphabet, 0) + alphabet_to_count[alphabet] += 1 + + hash_value = 0 + maximum_representable = 2 ** bit_span - 1 + for alphabet, count in alphabet_to_count.items(): + if count > maximum_representable: + raise ValueError("bit span is too small") + alphabet_count_value = count << (ord(alphabet) - ord("a")) * bit_span + hash_value = hash_value | alphabet_count_value + + return hash_value + + + + + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + hash_to_group = {} + for word in strs: + hash_value = self.calc_hash_by_alphabet_count(word) + hash_to_group.setdefault(hash_value, []) + hash_to_group[hash_value].append(word) + return list(hash_to_group.values()) + ``` \ No newline at end of file From d91bf362c120df6b611d9565b73a54f09701d0e7 Mon Sep 17 00:00:00 2001 From: Kazuki Kitano Date: Sun, 1 Mar 2026 19:34:10 +0900 Subject: [PATCH 6/6] memo --- memo.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/memo.md b/memo.md index cd08205..2b88284 100644 --- a/memo.md +++ b/memo.md @@ -281,4 +281,22 @@ class Solution: hash_to_group[hash_value].append(word) return list(hash_to_group.values()) -``` \ No newline at end of file +``` + +# 学んだこと + +* bitを使わなくても, `tuple`自体がhashableなことを利用すればもっと単純にかけた. +* やっていることは似ているが, 公式の`tuple`のハッシュ化の方が効率化されていそう + * https://github.com/python/cpython/blob/main/Objects/tupleobject.c + +```c + for (Py_ssize_t i = 0; i < len; i++) { + Py_uhash_t lane = PyObject_Hash(item[i]); + if (lane == (Py_uhash_t)-1) { + return -1; + } + acc += lane * _PyTuple_HASH_XXPRIME_2; + acc = _PyTuple_HASH_XXROTATE(acc); + acc *= _PyTuple_HASH_XXPRIME_1; + } +```