392. is subsequence#3
Open
lightbanana wants to merge 2 commits into
Open
Conversation
lightbanana
commented
Jun 16, 2026
Owner
- 今回解いた問題:392. Is Subsequence https://leetcode.com/problems/is-subsequence?envType=problem-list-v2&envId=xo2bgr0r
- 次に解く問題:141. Linked List Cycle https://leetcode.com/problems/linked-list-cycle?envType=problem-list-v2&envId=xo2bgr0r
nodchip
reviewed
Jun 18, 2026
| class Solution: | ||
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| index = 0 | ||
| if len(s) == 0: |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
mamo3gr/arai60#6 (comment)
Owner
Author
There was a problem hiding this comment.
ありがとうございます! Implicit False を用いることでコードが簡潔になることを確認しました。これからは使おうと思います。
nodchip
reviewed
Jun 18, 2026
| def isSubsequence(self, s: str, t: str) -> bool: | ||
| pattern = "" | ||
| for c in s: | ||
| pattern += ".*" + c |
There was a problem hiding this comment.
この書き方の場合、時間計算量が O(N2) かかる点に注意が必要です。 Python は文字列は Immutable で、文字列を連結するたびに新しいインスタンスが作られます。新しいインスタンスが作られる度に、既存の文字列をコピーし、新しい文字を連結するため、トータルで O(N2) かかります。
特定の条件下で in-place な処理に置き換わりますが、置き換わる条件は常識には含まれていないと思います。これを前提とした処理を書くのは避けたほうが無難だと思います。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。Pythonの文字列の連結は新しいインスタンスが作られるたびに既存の文字列をコピーし、時間計算量がO(n^2)になるため、避けることが無難であることを理解しました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.