-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...n/java/problemsolving/leetcode/algorithm/dynamicProgramming/wordBreak/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 139. Word Break | ||
|
||
Given a string `s` and a dictionary of strings `wordDict`, | ||
return true if `s` can be segmented into a space-separated | ||
sequence of one or more dictionary words. | ||
|
||
Note that the same word in the dictionary may be reused multiple times in the segmentation. | ||
|
||
### Example 1 | ||
``` | ||
Input: s = "leetcode", wordDict = ["leet","code"] | ||
Output: true | ||
Explanation: Return true because "leetcode" can be segmented as "leet code". | ||
``` | ||
|
||
### Example 2 | ||
``` | ||
Input: s = "applepenapple", wordDict = ["apple","pen"] | ||
Output: true | ||
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple". | ||
Note that you are allowed to reuse a dictionary word. | ||
``` | ||
|
||
### Example 3 | ||
``` | ||
Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"] | ||
Output: false | ||
``` | ||
|
||
## Constraints | ||
* `1 <= s.length <= 300` | ||
* `1 <= wordDict.length <= 1000` | ||
* `1 <= wordDict[i].length <= 20` | ||
* `s` and `wordDict[i]` consist of only lowercase English letters. | ||
* All the strings of wordDict are unique. | ||
|
||
# Solution Reference | ||
https://www.youtube.com/watch?v=Sx9NNgInc3A&ab_channel=NeetCode | ||
https://youtu.be/oBt53YbR9Kk?t=7967 |
28 changes: 28 additions & 0 deletions
28
...a/problemsolving/leetcode/algorithm/dynamicProgramming/wordBreak/WordBreakBruteForce.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.wordBreak; | ||
|
||
import java.util.List; | ||
|
||
public class WordBreakBruteForce { | ||
public boolean wordBreak(String s, List<String> wordDict) { | ||
if (s.length() == 0) { | ||
return true; | ||
} | ||
|
||
// System.out.println("Searching for word " + s); | ||
String wordToSearch = s; | ||
for (String word: wordDict) { | ||
if (wordToSearch.startsWith(word)) { | ||
// System.out.println("Found the word " + word + ": " + wordToSearch); | ||
String newWordToSearch = wordToSearch.substring(word.length()); | ||
|
||
boolean output = wordBreak(newWordToSearch, wordDict); | ||
if (output == true) { | ||
// System.out.println("Returning true for word " + s); | ||
return true; | ||
} | ||
} | ||
} | ||
// System.out.println("Returning false for word " + s); | ||
return false; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...msolving/leetcode/algorithm/dynamicProgramming/wordBreak/WordBreakDynamicProgramming.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.wordBreak; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class WordBreakDynamicProgramming { | ||
public boolean wordBreak(String s, List<String> wordDict) { | ||
Map<String, Boolean> memo = new HashMap<>(); | ||
return wordBreak(s, wordDict, memo); | ||
} | ||
|
||
private boolean wordBreak(String s, List<String> wordDict, Map<String, Boolean> memo) { | ||
if (s.length() == 0) { | ||
return true; | ||
} | ||
|
||
if (memo.containsKey(s)) { | ||
return memo.get(s); | ||
} | ||
|
||
// System.out.println("Searching for word " + s); | ||
for (String word: wordDict) { | ||
if (s.startsWith(word)) { | ||
String newWordToSearch = s.substring(word.length()); | ||
|
||
boolean output = wordBreak(newWordToSearch, wordDict, memo); | ||
if (output) { | ||
memo.put(s, true); | ||
return true; | ||
} | ||
} | ||
} | ||
memo.put(s, false); | ||
return false; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...oblemsolving/leetcode/algorithm/dynamicProgramming/wordBreak/WordBreakBruteForceTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.wordBreak; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class WordBreakBruteForceTest { | ||
@Test | ||
public void testCase01() { | ||
String word = "leetcode"; | ||
List<String> wordDict = Arrays.asList("leet","code"); | ||
|
||
boolean output = new WordBreakBruteForce().wordBreak(word, wordDict); | ||
|
||
assertTrue(output); | ||
} | ||
|
||
@Test | ||
public void testCase02() { | ||
String word = "applepenapple"; | ||
List<String> wordDict = Arrays.asList("apple","pen"); | ||
|
||
boolean output = new WordBreakBruteForce().wordBreak(word, wordDict); | ||
|
||
assertTrue(output); | ||
} | ||
|
||
@Test | ||
public void testCase03() { | ||
String word = "catsandog"; | ||
List<String> wordDict = Arrays.asList("cats","dog","sand","and","cat"); | ||
|
||
boolean output = new WordBreakBruteForce().wordBreak(word, wordDict); | ||
|
||
assertFalse(output); | ||
} | ||
|
||
@Test | ||
@Disabled("This test takes 46 seconds. Need to improve in DP version") | ||
public void testCase04() { | ||
String word = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; | ||
List<String> wordDict = Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", | ||
"aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"); | ||
|
||
boolean output = new WordBreakBruteForce().wordBreak(word, wordDict); | ||
|
||
assertFalse(output); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ving/leetcode/algorithm/dynamicProgramming/wordBreak/WordBreakDynamicProgrammingTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.wordBreak; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class WordBreakDynamicProgrammingTest { | ||
@Test | ||
public void testCase01() { | ||
String word = "leetcode"; | ||
List<String> wordDict = Arrays.asList("leet","code"); | ||
|
||
boolean output = new WordBreakDynamicProgramming().wordBreak(word, wordDict); | ||
|
||
assertTrue(output); | ||
} | ||
|
||
@Test | ||
public void testCase02() { | ||
String word = "applepenapple"; | ||
List<String> wordDict = Arrays.asList("apple","pen"); | ||
|
||
boolean output = new WordBreakDynamicProgramming().wordBreak(word, wordDict); | ||
|
||
assertTrue(output); | ||
} | ||
|
||
@Test | ||
public void testCase03() { | ||
String word = "catsandog"; | ||
List<String> wordDict = Arrays.asList("cats","dog","sand","and","cat"); | ||
|
||
boolean output = new WordBreakDynamicProgramming().wordBreak(word, wordDict); | ||
|
||
assertFalse(output); | ||
} | ||
|
||
@Test | ||
public void testCase04() { | ||
String word = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; | ||
List<String> wordDict = Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", | ||
"aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"); | ||
|
||
boolean output = new WordBreakDynamicProgramming().wordBreak(word, wordDict); | ||
|
||
assertFalse(output); | ||
} | ||
|
||
@Test | ||
public void testCase05_leetCode() { | ||
String word = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; | ||
List<String> wordDict = Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", | ||
"aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"); | ||
|
||
boolean output = new WordBreakDynamicProgramming().wordBreak(word, wordDict); | ||
|
||
assertFalse(output); | ||
} | ||
} |