Create 0392-is-subsequence.md - #58
Open
naoto-iwase wants to merge 1 commit into
Open
Conversation
nodchip
reviewed
Nov 9, 2025
|
|
||
| def build_char_positions_map(self, t: str) -> None: | ||
| self.char_to_positions = collections.defaultdict(list) | ||
| for i, char_ in enumerate(t): |
There was a problem hiding this comment.
Python には char 型はないため、 _ を付ける必要はないと思います。一方、他の言語だと型名として予約語になっているため、別の単語を用いたほうが無難だと思います。 c まあたは ch あたりはいかがでしょうか?
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
ご推察の通りの葛藤でした。
PEP8で、
If a function argument’s name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)
https://peps.python.org/pep-0008/#function-and-method-arguments
というのをみたことがあったため、_をつける方法を選択しました。
しかしcやchも割と見かける気がするので、確かに良いかもしれません。
tokuhirat
reviewed
Nov 10, 2025
Comment on lines
+186
to
+199
| last_positions = {} | ||
| previous = -1 | ||
| for char_ in s: | ||
| positions = self.char_to_positions.get(char_) | ||
| last = last_positions.get(char_, -1) | ||
| if positions is None: | ||
| return False | ||
| index = bisect.bisect_right(positions, previous, lo=last + 1) | ||
| if index == len(positions): | ||
| return False | ||
| last_positions[char_] = index | ||
| previous = positions[index] | ||
|
|
||
| return True |
There was a problem hiding this comment.
last が、ある文字に関して最後にマッチした index になっていると読みましたが、単に直近の index で良いように思います。
last_used_index = -1
for c in s:
positions_in_t = self.char_to_positions.get(c)
if positions_in_t is None:
return False
next_position_index = bisect.bisect_right(positions_in_t, last_used_index)
if next_position_index == len(positions_in_t):
return False
last_used_index = positions_in_t[next_position_index]
return True
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/
次の問題:https://leetcode.com/problems/next-permutation/