Conversation
| if (s.length() != t.length()) { | ||
| return false; | ||
| } | ||
| unordered_map<char, int> count_char; |
There was a problem hiding this comment.
unordered_mapがmapと比べて常に早いというわけではないようです。
There was a problem hiding this comment.
lookupの計算量がmapだとO(logn)でunorderd_mapだとO(1)ですが、ハッシュ化の定数倍の処理などがあるからnがすごく大きくない限りはそこまで変わらないみたいな感じですね。挿入についても同様。あとはハッシュテーブルで実際の要素より多くメモリを確保する必要があるのでそのオーバーヘッドもありますね。
| @@ -0,0 +1,39 @@ | |||
| # 1st | |||
There was a problem hiding this comment.
自分が解いたときは、 s と t をそれぞれソートして、一致するかどうかを判定しました。この方法でも書いてみていただけますか?
| ++count_letter_2[c]; | ||
| } | ||
|
|
||
| // アナグラムの判定, s に存在する文字が t に同数含まれているか |
There was a problem hiding this comment.
含まれる文字数が同じことを示せればよいので、 return count_letter_1 == count_letter2; でよいと思います。
| ++count_char_s[c]; | ||
| } | ||
|
|
||
| for (const auto& c : t) { |
| public: | ||
| bool isAnagram(string s, string t) { | ||
|
|
||
| map<char, int> count_letter_1; // counting_letter, letters_to_count |
There was a problem hiding this comment.
個人的には、キーが letter、値が count であることを表すため、 letter_to_count とすると思います。
| ++count_char[s[i]]; | ||
| --count_char[t[i]]; | ||
| } | ||
| for (const auto& kv: count_char) { |
There was a problem hiding this comment.
for (auto [letter, count] : count_char) { で回したほうが、コードが読みやすくなると思います。
| --count_char[t[i]]; | ||
| } | ||
| for (const auto& kv: count_char) { | ||
| if(kv.second != 0) { |
There was a problem hiding this comment.
if のあとに空白を 1 つ空けることをお勧めいたします。
https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching
The components of the statement should be separated by single spaces (not line breaks).
https://leetcode.com/problems/valid-anagram/description/