Skip to content

Conversation

hayashi-ay
Copy link
Owner

def firstUniqChar(self, s: str) -> int:
freq = [0] * 26
for c in s:
freq[ord(c) - ord('a')] += 1

Choose a reason for hiding this comment

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

listを使った解法にしたんですね。
想定外の文字列が入ってたらエラーになるので、dictのほうが安全かつ分かりやすいと思いました。
今回は英アルファベット小文字のみの制約があるので、気にし過ぎたコメントかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。

想定外の文字列が入ってたらエラーになるので、dictのほうが安全かつ分かりやすいと思いました。

まあ何を想定するかですかねー。英アルファベット小文字が来ることが担保できる状況ならハッシュ処理が入らない固定長のlistを使った方が早いと思ったのでlistの解法にしました。アルファベットをインデックス値に変換する処理が入るのでその分複雑にはなりますね。

dictは前提条件が変わって文字種が増えた際などにそのまま使い回せるので良いと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

半年後に読んだ別の同僚が不安にならないか、環境の変化に対して頑健か、なのです。

まあでもとりあえず書くならdictの方が良い気がしてきました。そのうえで入力値の制約下でパフォーマンスを向上させたいなどの要件があった際にlistを使った方を書く方がいいですね。

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.

2 participants