Skip to content

387. First Unique Character in a String - #3

Open
tsadamor wants to merge 1 commit into
mainfrom
0387-first-unique-char-in-str
Open

387. First Unique Character in a String#3
tsadamor wants to merge 1 commit into
mainfrom
0387-first-unique-char-in-str

Conversation

@tsadamor

Copy link
Copy Markdown
Owner

```python
class Solution2:
def firstUniqChar(self, s: str) -> int:
seen_index:dict[str, int] = {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

コロンと型名の間には空白を入れるのが一般的だと思います。

seen_index: dict[str, int] = {}

class Solution2:
def firstUniqChar(self, s: str) -> int:
seen_index:dict[str, int] = {}
duplicated = -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.

Pythonでも、他の言語と同様定数は大文字で記載するのが一般的です。

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.

Google C++ Style Guide では、定数は kConstantValue の形で記載することになっています。

https://google.github.io/styleguide/cppguide.html#Constant_Names

Variables declared constexpr or const, and whose value is fixed for the duration of the program, are named with a leading "k" followed by mixed case.

ただ、この命名規約は、かなりマイナーだともいます。

- 一回文字列を頭から舐めて文字:出現数で辞書に登録
- もう一度文字列を走査して、出現数が1の文字のインデックス(かなければ-1)を返す
- 時間、空間ともにO(n)
- 入力が<= 10^5であることを考えるとPythonでも0.1秒収まるくらい?(平均的な処理能力を10^8/sec、Pythonがそこから最悪100倍で見積もっています、初めてなので勘違いしている点やリファレンスがあればコメントください)

@h-masder h-masder Jul 19, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

実行時間については、以下が参照されることが多いです。
Yuto729/leetcode#16 (comment)

数字については、根拠を大事にするとよいと思います。
(「平均的な処理能力を10^8/sec、Pythonがそこから最悪100倍」という部分ですね)

上記のURLでも、CPUの周波数、IPC、スレッド数など、いくつかの仮定を置いています。

また、「最悪100倍」、という見積もりの根拠として、上記のURLでは言語ごとの処理速度が紹介されていますが、その結果を自分自身がどのように利用しているのかは、意識しておく必要があると思います。

例えば、同じ「PythonはC++と比べて100倍遅い」という結果を利用する場合でも、根拠は様々です。

言語ごとの処理速度をみると、「PythonはC++と比べて100倍くらい遅い」ことが分かる。多くの人が参照しているため、この結果を利用することにした。

言語ごとの処理速度を確認したところ、READMEに測定条件(Rules)が記載されており、自分が想定する計算環境と大きく乖離していない。そこで、「PythonはC++と比べて100倍遅い」という結果を利用することにした。ただし、ソースコードは確認していない。

・実際にソースコードを確認した結果、測定条件や比較方法が妥当であることを確認できたため、そこから得られる「PythonはC++と比べて100倍遅い」という結果を利用することにした。

・実際に自分自身で同じ条件を再現して測定したところ、実際にC++とPythonは100倍程度の差が確認できたため、この結果を利用することにした。

自分がなぜそう結論付けたのか、説明できることが重要だと思います。

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.

ご丁寧にありがとうございます。
一旦は、

言語ごとの処理速度をみると、「PythonはC++と比べて100倍くらい遅い」ことが分かる。多くの人が参照しているため、この結果を利用することにした

このスタンス(根拠)とし、追ってソースコードの確認や自分での再現を行い、計算量に関する感覚や説明を磨いていこうと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

原理が重要な時もありまして、例えば、組み込み関数を呼んだ中が律速のときは、100倍にならないです。個人的には文字列操作は1倍、整数操作は20倍くらいで考えてます。

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.

5 participants