Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions 139/step1.cpp
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

bool がデフォルト初期化されると false になるため、引数の false は省略してよいと思います。

for (int i = 0; i < s.size(); ++i) {
if (i > 0 && matched_index[i - 1] == false) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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();
}
};
20 changes: 20 additions & 0 deletions 139/step2.cpp
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();
}
};
17 changes: 17 additions & 0 deletions 139/step3.cpp
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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここ気になったのですが、
i + word.size() - 1がsのインデックスの範囲を超えてしまったらどうなるんでしょう。

out of rangeが投げられるのですかね。

仕様書見た感じ大丈夫そうでした。

  1. Compares a [pos1, pos1 + count1) substring of this string to str.
    If count1 > size() - pos1, the substring is [pos1, size()).

https://en.cppreference.com/cpp/string/basic_string/compare

segmented_indexes[i + word.size() - 1] = true;
}
}
}
return segmented_indexes.back();
}
};