Skip to content

Commit

Permalink
Solved LeetCode Word Break Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsatpute committed Apr 19, 2023
1 parent e44b39b commit 821382a
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
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
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;
}
}
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;
}
}
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);
}
}
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);
}
}

0 comments on commit 821382a

Please sign in to comment.