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
33 changes: 33 additions & 0 deletions WordBreak/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
```java
// DP
// Create a boolean array with same length of given s
// Then foreach index of the DP, check if can we reach it
// Time complexity: O(N * M) -> N: length of given s, M: Number of unique lengths of the given word
// Space complexity: O(N + M + O) O: number of unique given word
// Time spend: 14:30
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>();
Set<Integer> wordLengthSet = new HashSet<>();
for (String word : wordDict) {
wordSet.add(word);
wordLengthSet.add(word.length());
}

boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;

for (int i = 0; i < dp.lenght; i++) {
for (Integer wordLength : wordLengthSet) {
int previousIndex = i - wordLength;
if (previousIndex >= 0 && dp[previousIndex] && wordSet.contains(s.substring(previousIndex, i))) {
dp[i] = true;
break;
}
}
}

return dp[s.length()];
}
}
```
29 changes: 29 additions & 0 deletions WordBreak/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
```java
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この問題は、いくらか解き方や考えるところあると思うので、調べておいてください。

か、誰かレビューワー指摘してあげてください。

// Time spend: 05:39
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>();
Set<Integer> wordLengthSet = new HashSet<>();

for (String word : wordDict) {
wordSet.add(word);
wordLengthSet.add(word.length());
}

var dp = new boolean[s.length() + 1];
dp[0] = true;

for (int i = 1; i <= s.length(); i++) {
for (int wordLength : wordLengthSet) {
var previousIndex = i - wordLength;
if (previousIndex >= 0 && dp[previousIndex] && wordSet.contains(s.substring(previousIndex, i))) {
dp[i] = true;
break;
}
}
}

return dp[s.length()];
}
}
```
89 changes: 89 additions & 0 deletions WordBreak/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
```java
// Time spend: 03:59
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>();
Set<Integer> wordLengthSet = new HashSet<>();
for (String word : wordDict) {
wordSet.add(word);
wordLengthSet.add(word.length());
}

int l = s.length();
var dp = new boolean[l + 1];
dp[0] = true;
for (int i = 1; i <= l; i++) {
for (int length : wordLengthSet) {
var previousIndex = i - length;
if (previousIndex >= 0 && dp[previousIndex] && wordSet.contains(s.substring(previousIndex, i))) {
dp[i] = true;
break;
}
}
}

return dp[l];
}
}
```

```java
// Time spend: 02:59
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>();
Set<Integer> wordLengthSet = new HashSet<>();
for (String word : wordDict) {
wordSet.add(word);
wordLengthSet.add(word.length());
}

int l = s.length();
var dp = new boolean[l + 1];
dp[0] = true;
for (int i = 1; i <= l; i++) {
for (int length : wordLengthSet) {
var previousIndex = i - length;
if (previousIndex >= 0 && dp[previousIndex] && wordSet.contains(s.substring(previousIndex, i))) {
dp[i] = true;
break;
}
}
}

return dp[l];
}
}
```

```java
// Time spend: 02:40
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordSet = new HashSet<>();
Set<Integer> wordLengthSet = new HashSet<>();
for (String word : wordDict) {
wordSet.add(word);
wordLengthSet.add(word.length());
}

int l = s.length();
boolean[] dp = new boolean[l + 1];
dp[0] = true;
for (int i = 1; i <= l; i++) {
for (int length : wordLengthSet) {
int previousIndex = i - length;
if (previousIndex < 0 || !dp[previousIndex]) {
continue;
}
if (wordSet.contains(s.substring(previousIndex, i))) {
dp[i] = true;
break;
}
}
}

return dp[l];
}
}
```