Skip to content

387. First Unique Character in a String#20

Merged
ryoooooory merged 11 commits intomainfrom
task/387
Sep 22, 2024
Merged

387. First Unique Character in a String#20
ryoooooory merged 11 commits intomainfrom
task/387

Conversation

@ryoooooory
Copy link
Copy Markdown
Owner

class Solution {
// leetcode
public int firstUniqChar(String s) {
Map<Character, Integer> indices = new HashMap<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名 indices から、文字と出現頻度のマップだということが分かりにくく感じました。 charcterToCountscharacterToFrequencies といった、文字と出現頻度のマップであることが想起しやすい変数名を付けることをお勧めいたします。

class Solution {
// leetcode
public int firstUniqChar(String s) {
Map<Character, Integer> indices = new HashMap<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step1-2 以降のように、配列を使ったほうが処理が軽くなると思います。

class Solution {
// leetcode
public int firstUniqChar(String s) {
int[] cs = new int[26];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

cs という変数名からは、中にどのような値が入っているかを推測するのが難しく感じました。 step 2-1 のように推測しやすい英単語、または英語句を使用するとよいと思います。また、配列やコレクションのように、複数の値が含まれる変数については、最後の単語を複数形にすると、理解しやすくなると思います。

@@ -0,0 +1,21 @@
class Solution {
// leetcode
private static final int ALPHABET_SIZE = 26;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら NUM_ALPHABETS と名付けると思います。ここは好みかもしれません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

小文字のアルファベットの数のことなので、大きさを表す size は適当でないように思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

このままでも良さそうです。https://csrc.nist.gov/glossary/term/alphabet_size

Copy link
Copy Markdown

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.

ちょっと上記のソース読んでみたのですが、どうやら上記の alphabet とは一般的に想起される abc... とは違うみたいです。全く専門外なのであまり自信がないですが、ソースになっている論文での使われ方を見るに size は可変であるようなので、恐らく形式言語における alphabet の事を指しているのかと思われます。

abc... の方の alphabet について、これの size が a-z の数26を指すという例は、私の方で少しググった限りでは見つけられませんでした。
ただ wikipedia に "An alphabet may have any cardinality ("size") and, depending on its purpose, may be finite (e.g., the alphabet of letters "a" through "z")" とあるように、形式言語における alphabet は abc... の方の alphabet も指し得るみたいなので、一般的かはさておき間違いではないですね。

Copy link
Copy Markdown

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.

https://www.ldoceonline.com/dictionary/alphabet

a set of letters, arranged in a particular order, and used in writing

setに対してsizeを使うのは、普通の表現だと思います。
例:https://zoo.cs.yale.edu/classes/cs467/2005f/course/handouts/ho15.pdf
image

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setに対してsizeを使うのは、普通の表現だと思います。

なるほど確かに。

return i;
}
}
return NOT_FOUND;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCodeの制約上仕方がないですが、そもそも-1を返すことが良いことではないので、これが本当に良いのか(根本的な解決になっていないので)については個人的には疑問があります。それよりもLeetCodeの制約がないのであれば、どのように書くかということについて論じる(または実際に書いてみる)ほうが良いのではないかと思います。

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.

おっしゃるとおり実際のシステムだったら
・-1を返して呼び出し側でエラーチェックする
・この関数でエラーを返す
・そもそも入力値のバリデーション関数を別で設ける
といったことが候補に上がるかなと思っています。(結構既存のシステムのお作法(通例)にも影響される部分もあるイメージ)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あ、はい、ここは「使う人の気持ちになって私はこうするのが一番いいと思ったのでこうしたのだ、もしかしたらもっといい方法があるのかもしれないが、少なくとも私はそう判断したのだ」と言えるならばいいと思います。

小学校の低学年の頃に、母に「きれいな字を書けるかは能力であるから人による。しかし、誠実な字を書くことは誰にでもできる。誠実な字とは読み手に伝えようという意思を持った字である。」といわれたことを思い出します。

public static final int NOT_FOUND = -1;

public int firstUniqChar(String s) {
int[] frequency = new int[ALPHABET_SIZE];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

頻度の配列であることがわかるようにfrequenciesと複数形にすべきではないかと、自分は以前にnodaさんからコメントをもらいました

rihib/leetcode#34 (comment)

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.

6 participants