Conversation
| s = "a\u0308" # 'a' + '̈' | ||
| print(s) # 出力: ä | ||
| print(len(s)) # 出力: 2 | ||
| ``` |
There was a problem hiding this comment.
ご存知かもしれませんが、同じウムラウト付きの文字でも、1文字のもの "ä" ("\u00e4") があります。
unicodedata.normalize を見ておくといいかもしれません。
unicodedata.normalize("NFC", "が")
'が'
len(unicodedata.normalize("NFC", "が"))
1
unicodedata.normalize("NFD", "が")
'が'
len(unicodedata.normalize("NFD", "が"))
2| return -1 | ||
| ``` | ||
|
|
||
| Counter を使用した版 |
There was a problem hiding this comment.
https://docs.python.org/3/library/collections.html#collections.Counter
挿入順番が保存されるようになったのは3.7からですね。
There was a problem hiding this comment.
linked list + hashmap という Java の LinkedHashMap や LRU などで用いられる構造なので、どこかで一回書いておいてください。
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
とてもよく聞かれます。
https://discord.com/channels/1084280443945353267/1200089668901937312/1206182044292485171
There was a problem hiding this comment.
ありがとうございます。step4で書いてみます。
|
|
||
| 辞書ではなくリストを使った版 | ||
| この場合、配列を使用しているので、インデックスによるアクセスにより、辞書よりも早くなる可能性がある。 | ||
| その一方で、(制約では英小文字しかこないが)英小文字以外の文字が来ることも考えると辞書のほうが安全。 |
There was a problem hiding this comment.
英小文字でないものが来たときに、このコードだとどうなるか、その振る舞いでいいのかはちょっと考えてみましょう。
| for char in s: | ||
| if char not in char_to_counts: | ||
| char_to_counts[char] = 0 | ||
| char_to_counts[char] += 1 |
There was a problem hiding this comment.
最終的にchar_to_countsの取得をこの方法で行ったのはなぜですか?Counterを使う方法が一番簡潔で、defaultdictを使うと条件分岐が要らなくなるので自分だったらその二つのいずれかを使うと思いました。
https://leetcode.com/problems/first-unique-character-in-a-string/description/