Skip to content

392. is subsequence#3

Open
lightbanana wants to merge 2 commits into
mainfrom
392.-Is-Subsequence
Open

392. is subsequence#3
lightbanana wants to merge 2 commits into
mainfrom
392.-Is-Subsequence

Conversation

@lightbanana

Copy link
Copy Markdown
Owner

Comment thread memo.md
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
index = 0
if len(s) == 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
mamo3gr/arai60#6 (comment)

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.

ありがとうございます! Implicit False を用いることでコードが簡潔になることを確認しました。これからは使おうと思います。

Comment thread memo.md
def isSubsequence(self, s: str, t: str) -> bool:
pattern = ""
for c in s:
pattern += ".*" + c

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この書き方の場合、時間計算量が O(N2) かかる点に注意が必要です。 Python は文字列は Immutable で、文字列を連結するたびに新しいインスタンスが作られます。新しいインスタンスが作られる度に、既存の文字列をコピーし、新しい文字を連結するため、トータルで O(N2) かかります。
特定の条件下で in-place な処理に置き換わりますが、置き換わる条件は常識には含まれていないと思います。これを前提とした処理を書くのは避けたほうが無難だと思います。

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の文字列の連結は新しいインスタンスが作られるたびに既存の文字列をコピーし、時間計算量がO(n^2)になるため、避けることが無難であることを理解しました。

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