Conversation
thonda28
reviewed
Jun 8, 2024
競技プロ就活部PR用/392. Is Subsequence.md
Outdated
Comment on lines
9
to
20
There was a problem hiding this comment.
こちら s に番兵を追加して for ループを回し、最後に番兵に到達しているかどうかを返すという実装方法もありそうです。
その場合、if s == "": の条件分岐と for 文中の if s_index == len(s): の条件が不要になるので、コードとしてはシンプルに書けそうに思いました。
Owner
Author
There was a problem hiding this comment.
ありがとうございます。なるほどと思いました。
追記してコミットしてみましたので、よければ一目見ていただける嬉しいです。
There was a problem hiding this comment.
コメント時に想定していたものとほぼ同じでした、よさそうです。参考までに僕のコードも載せておきます。(for ループか while ループかの違いだけですが)
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
s += "#"
s_index = 0
for t_index in range(len(t)):
if t[t_index] == s[s_index]:
s_index += 1
return s[s_index] == "#"
関数型(っぽい)、番兵の追加の解法を追記
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.
https://leetcode.com/problems/is-subsequence/description/