Skip to content

392. Is Subsequence#52

Open
tom4649 wants to merge 2 commits intomainfrom
392.Is-Subsequence
Open

392. Is Subsequence#52
tom4649 wants to merge 2 commits intomainfrom
392.Is-Subsequence

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Apr 23, 2026

Comment on lines +13 to +14
if index_s == len(s):
return True
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは、index_sをインクリメントした時以外は判定不要なので、上のifの中に入れた方が良さそうです。

index_s += 1
if index_s == len(s):
return True
index_t += 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.

毎回のiterationで、+1するので、forループで書き換えた方が良さそうです。

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.

なるほど、どちらもご指摘の通りです。自分でも見直してこうした点に気づけるようになりたいです。


index_s = 0
index_t = 0
while len(s) - index_s <= len(t) - index_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.

制約を見ると、sは短めなので、early returnするメリットはあまりないかもしれませんね。

0 <= s.length <= 100
0 <= t.length <= 104

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.

速度を簡単に測ってみましたが大きな差はなく sol3.pyの方が遅い場合もありました。
高速化したつもりがそうなっていない場合もあるんですね。

sol1: 1532.053 ms
sol3: 1710.168 ms
(sol3 / sol1: 1.116)

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