Skip to content

Conversation

@skypenguins
Copy link
Owner

392. Is Subsequence

次回予告: 139. Word Break

@skypenguins skypenguins self-assigned this Jun 3, 2025
* https://github.com/fuga-98/arai60/blob/27af9937ef75a7565d13d27e79853dd0f93fa070/392.%20Is%20Subsequence.md#step1
- ループの途中でreturnして、なるべく早く制御を戻している
* https://github.com/olsen-blue/Arai60/blob/0aac26e68fa6b8f516ff494da34578b8cc1cefc2/392.%20Is%20Subsequence.md#%E8%A7%A3%E6%B3%952%E4%BB%95%E4%BA%8B%E3%81%AE%E5%BC%95%E3%81%8D%E7%B6%99%E3%81%8E%E3%83%88%E3%83%83%E3%83%97%E3%83%80%E3%82%A6%E3%83%B3%E5%86%8D%E5%B8%B0dpac
- 解法2は再帰を使った方法。視線誘導がすっきりしていて読みやすい。スタックオーバーフローにならないか気になった。
Copy link

Choose a reason for hiding this comment

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

再帰の深さの限界についてはこちらを見ておくとよさそうです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.uvguf4c3q02d

return True
if s[idx] == char_t:
idx += 1
return idx == len(s)
Copy link

Choose a reason for hiding this comment

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

略語の使用についてはこちらを参照すると良いかもしれません。
https://discord.com/channels/1084280443945353267/1357969871673102446/1367455303582355496

```python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
idx = 0
Copy link

Choose a reason for hiding this comment

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

単語から文字を削って変数名を付けた場合、読み手に取って認知負荷が上がる場合があります。原則フルスペルで付けることをお勧めいたします。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/pyguide.html#316-naming

Avoid abbreviation. In particular, do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word.

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
idx = 0
for char_t in t:
Copy link

Choose a reason for hiding this comment

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

chat_t という変数名は、 C++ の標準ライブラリの構造体名を連想させ、やや紛らわしく感じました。単に c または ch で十分だと思います。

- ①はエッジケースがループに自然に組み込まれていていて読みやすい
- ②の正規表現を使う方法は面白いと思った
- ③は自分が当初やろうとしていた方針に近いかも。defaultdictを使ってリスト型で初期化し、リストを二分探索すればよかったのか…パズルに近いかも?
- ④は `find()` を使っているが、時間計算量が①と同じなのが意外だった
Copy link

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/stdtypes.html#str.find
公式ドキュメントを見ておきましょう。

if len(s) == 0:
return True

subseq_i = 0
Copy link

Choose a reason for hiding this comment

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

自分なら t_i と対応させて s_i とすると思いました。

@hroc135
Copy link

hroc135 commented Jun 15, 2025

一番外側のループで s を舐める場合でも書けるので書いてみるといいかもしれません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants