Open
Conversation
nodchip
reviewed
Jan 9, 2026
| can_split[right] = True | ||
| break | ||
|
|
||
| return can_split[len(s)] |
| ```py | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| word_set = set(wordDict) |
There was a problem hiding this comment.
自分は変数名には型名を入れないことが多いです。理由は、型名を入れても読み手にとって有益な情報にならないことが多いためです。ただ、今回は list 型と set 型の両方が同じスコープに存在しているため、区別のために、入れたほうが良いかもしれないと思いました。
Owner
Author
There was a problem hiding this comment.
とても勉強になります。ありがとうございます!
| can_split[0] = True | ||
|
|
||
| for right in range(1, len(s) + 1): | ||
| for left in range(right): |
There was a problem hiding this comment.
left と wordDict の文字列でループを回すという方法もあります。
can_split = [False] * (len(s) + 1)
can_split[0] = True
for left in range(len(s)):
if not can_split[left]:
continue
for word in wordDict:
if s.startswith(word, left):
can_split[left + len(word)] = True
return can_split[-1]
Owner
Author
There was a problem hiding this comment.
ああ、こっちのほうが直観的ですね。ありがとうございます!!
oda
reviewed
Jan 16, 2026
| @@ -0,0 +1,113 @@ | |||
| ## step1 | |||
|
|
|||
| 5分でわからず ChatGPT に答えを聞いた。以下の回答のような方針は頭によぎったが、二重ループで大丈夫なのかとか、実装が大変そうだななどの不安から手が動かなかった。 | |||
There was a problem hiding this comment.
このあたり、コストパフォーマンスに見合わないことをしているのではないか、という気持ちを押さえる能力は、たぶんあるんですよね。
日常においても、投機的に実行したり、探索をしたりすることはあるわけです。買い物に行って、「うーん、使わないかもしれないけれどもどうしても必要になる可能性がある。また買いに来る手間を考えると安いから買ってしまおう。」であったり、「おそらくここらへんに売っているから探しに行こう」であったりです。
こういう手段は日常的に取っているはずで、これができないと結構辛いはずです。
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/word-break/description/