Skip to content

Create 242. Valid Anagram#8

Open
kzhra wants to merge 4 commits intomainfrom
242.-Valid-Anagram
Open

Create 242. Valid Anagram#8
kzhra wants to merge 4 commits intomainfrom
242.-Valid-Anagram

Conversation

@kzhra
Copy link
Copy Markdown
Owner

@kzhra kzhra commented Jul 15, 2024

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思います。ソートするというのもありますね。

if (s.length() != t.length()) {
return false;
}
unordered_map<char, int> count_char;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unordered_mapがmapと比べて常に早いというわけではないようです。

Ryotaro25/leetcode_first60#10 (comment)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lookupの計算量がmapだとO(logn)でunorderd_mapだとO(1)ですが、ハッシュ化の定数倍の処理などがあるからnがすごく大きくない限りはそこまで変わらないみたいな感じですね。挿入についても同様。あとはハッシュテーブルで実際の要素より多くメモリを確保する必要があるのでそのオーバーヘッドもありますね。

@@ -0,0 +1,39 @@
# 1st
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分が解いたときは、 s と t をそれぞれソートして、一致するかどうかを判定しました。この方法でも書いてみていただけますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

書きましたのでレビューをお願いします

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

よいと思います。

++count_letter_2[c];
}

// アナグラムの判定, s に存在する文字が t に同数含まれているか
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

含まれる文字数が同じことを示せればよいので、 return count_letter_1 == count_letter2; でよいと思います。

++count_char_s[c];
}

for (const auto& c : t) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2-2 のほうがシンプルでよいと思います。

public:
bool isAnagram(string s, string t) {

map<char, int> count_letter_1; // counting_letter, letters_to_count
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には、キーが letter、値が count であることを表すため、 letter_to_count とすると思います。

++count_char[s[i]];
--count_char[t[i]];
}
for (const auto& kv: count_char) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (auto [letter, count] : count_char) { で回したほうが、コードが読みやすくなると思います。

--count_char[t[i]];
}
for (const auto& kv: count_char) {
if(kv.second != 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4thの回答良いと思います。

@rihib rihib mentioned this pull request Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants