Skip to content

Create 0392-is-subsequence.md - #58

Open
naoto-iwase wants to merge 1 commit into
mainfrom
0392-is-subsequence
Open

Create 0392-is-subsequence.md#58
naoto-iwase wants to merge 1 commit into
mainfrom
0392-is-subsequence

Conversation

@naoto-iwase

Copy link
Copy Markdown
Owner


def build_char_positions_map(self, t: str) -> None:
self.char_to_positions = collections.defaultdict(list)
for i, char_ in enumerate(t):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python には char 型はないため、 _ を付ける必要はないと思います。一方、他の言語だと型名として予約語になっているため、別の単語を用いたほうが無難だと思います。 c まあたは ch あたりはいかがでしょうか?

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.

ありがとうございます。

ご推察の通りの葛藤でした。

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も割と見かける気がするので、確かに良いかもしれません。

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

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.

3 participants