Skip to content

Commit

Permalink
refactor 139
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Jun 28, 2020
1 parent 5757623 commit 3574b2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 54 deletions.
51 changes: 17 additions & 34 deletions src/main/java/com/fishercoder/solutions/_139.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
package com.fishercoder.solutions;

import java.util.HashSet;
import com.fishercoder.common.utils.CommonUtils;

import java.util.List;
import java.util.Set;

public class _139 {

public static class Solution1 {
public boolean wordBreak(String s, List<String> wordDict) {
return wordBreak(s, new HashSet(wordDict), 0);
}

public boolean wordBreak(String s, Set<String> wordDict, int start) {
if (start == s.length()) {
return true;
}
for (int end = start + 1; end <= s.length(); end++) {
if (wordDict.contains(s.substring(start, end)) && wordBreak(s, wordDict, end)) {
return true;
}
}
return false;
}
}

public static class Solution2 {
/**
* this beats 70.46% submission.
* this solution takes between 7 and 8 ms to finish on LeetCode
* beats around 38% to 48% submissions as of 6/27/2020
*/
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && wordDict.contains(s.substring(j, i))) {
if (dp[j]
&&
wordDict.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
CommonUtils.printArray(dp);
return dp[n];
}
}

public static class Solution3 {
public static class Solution2 {
/**
* Added pruning.
* this beats 89.91% submissions.
* Added pruning based on max word length.
* this solution takes between 2 and 3 ms to finish on LeetCode
* this beats 94.53% submissions as of 6/27/2020
*/
public boolean wordBreak(String s, List<String> wordDict) {
int maxLen = Integer.MIN_VALUE;
Expand All @@ -73,10 +60,11 @@ public boolean wordBreak(String s, List<String> wordDict) {
}
}

public static class Solution4 {
public static class Solution3 {
/**
* Added pruning, plus start from the end to check.
* This beats 95.20% submissions.
* This solution takes 1 ms to finish on LeetCode
* This beats 99.02% submissions as of 6/27/2020.
*/
public boolean wordBreak(String s, List<String> wordDict) {
int maxLen = Integer.MIN_VALUE;
Expand All @@ -88,13 +76,8 @@ public boolean wordBreak(String s, List<String> wordDict) {
boolean[] dp = new boolean[n + 1];
dp[0] = true;
for (int i = 1; i <= n; i++) {
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen;
lastWordLength++) {
if (!dp[i - lastWordLength]) {
continue;
}
String sub = s.substring(i - lastWordLength, i);
if (wordDict.contains(sub)) {
for (int lastWordLength = 1; lastWordLength <= i && lastWordLength <= maxLen; lastWordLength++) {
if (dp[i - lastWordLength] && wordDict.contains(s.substring(i - lastWordLength, i))) {
dp[i] = true;
break;
}
Expand Down
22 changes: 2 additions & 20 deletions src/test/java/com/fishercoder/_139Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class _139Test {
private static _139.Solution1 solution1;
private static _139.Solution2 solution2;
private static _139.Solution3 solution3;
private static _139.Solution4 solution4;
private static String s;
private static List<String> wordDict;

Expand All @@ -24,7 +23,6 @@ public static void setup() {
solution1 = new _139.Solution1();
solution2 = new _139.Solution2();
solution3 = new _139.Solution3();
solution4 = new _139.Solution4();
}

@Test
Expand All @@ -50,39 +48,23 @@ public void test3() {

@Test
public void test4() {
s = "leetcode";
wordDict = new ArrayList<>(Arrays.asList("leet", "code"));
assertEquals(true, solution4.wordBreak(s, wordDict));
}

@Test
@Ignore
public void test5() {
// this one will time out due to the inefficient algorithm, so ignore it
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
assertEquals(false, solution1.wordBreak(s, wordDict));
}

@Test
public void test6() {
public void test5() {
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
assertEquals(false, solution2.wordBreak(s, wordDict));
}

@Test
public void test7() {
public void test6() {
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
assertEquals(false, solution3.wordBreak(s, wordDict));
}

@Test
public void test8() {
s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
wordDict = new ArrayList<>(Arrays.asList("a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa", "aaaaaaaaaa"));
assertEquals(false, solution4.wordBreak(s, wordDict));
}

}

0 comments on commit 3574b2f

Please sign in to comment.