-
Notifications
You must be signed in to change notification settings - Fork 0
139. Word Break #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
139. Word Break #51
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| Time: 20:30 | ||
|
|
||
| Time Complexity: O(n * m *l) (n: sの長さ, m: wordDictの長さ, l: wordDictの単語平均長) | ||
| Space Complexity: O(1) | ||
|
|
||
| 全探索的な解法はすぐに思いついたが、細かいミスで時間をロスした | ||
| C++の文字列操作関連でいい感じのメソッドを知っていればそれを使用したが、一旦手慣れたループで対応 | ||
| */ | ||
|
|
||
| class Solution { | ||
| public: | ||
| bool wordBreak(string s, vector<string>& wordDict) { | ||
| vector<bool> matched_index(s.size(), false); | ||
| for (int i = 0; i < s.size(); ++i) { | ||
| if (i > 0 && matched_index[i - 1] == false) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. matched_index[0] = true で初期化しておくと、 i > 0 を消せてシンプルになると思います。 |
||
| continue; | ||
| } | ||
| for (string word : wordDict) { | ||
| if (word[0] != s[i]) { | ||
| continue; | ||
| } | ||
| for (int j = 0; j < word.size(); ++j) { | ||
| if (i + j >= s.size()) { | ||
| break; | ||
| } | ||
| if (s[i + j] != word[j]) { | ||
| break; | ||
| } | ||
| if (j == word.size() - 1) { | ||
| matched_index[i + j] = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return matched_index.back(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| string_viewを使用する方法もあったが、変数を増やすよりもcompareで直接比較したほうがシンプル | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool wordBreak(string s, vector<string>& wordDict) { | ||
| vector<uint8_t> segmented_indexes(s.size(), false); | ||
| for (int i = 0; i < s.size(); ++i) { | ||
| if (i > 0 && segmented_indexes[i - 1] == false) { | ||
| continue; | ||
| } | ||
| for (const string& word: wordDict) { | ||
| if (!s.compare(i, word.size(), word)) { | ||
| segmented_indexes[i + word.size() - 1] = true; | ||
| } | ||
| } | ||
| } | ||
| return segmented_indexes.back(); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution { | ||
| public: | ||
| bool wordBreak(string s, vector<string>& wordDict) { | ||
| vector<uint8_t> segmented_indexes(s.size()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 細かいですが、indexの複数形indicesです! あと、indexが格納されているわけではないので、reachableみたいに自分は名付けました。 他は良いと思いました。 |
||
| for (int i = 0; i < s.size(); ++i) { | ||
| if (i > 0 && !segmented_indexes[i - 1]) { | ||
| continue; | ||
| } | ||
| for (const string& word : wordDict) { | ||
| if (s.compare(i, word.size(), word) == 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここ気になったのですが、 out of rangeが投げられるのですかね。 仕様書見た感じ大丈夫そうでした。
|
||
| segmented_indexes[i + word.size() - 1] = true; | ||
| } | ||
| } | ||
| } | ||
| return segmented_indexes.back(); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool がデフォルト初期化されると false になるため、引数の false は省略してよいと思います。