Skip to content

Commit 01872ff

Browse files
[N-0] refactor 139
1 parent fe01434 commit 01872ff

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/main/java/com/fishercoder/solutions/_139.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/**
77
* 139. Word Break
8+
*
89
* Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
910
* determine if s can be segmented into a space-separated sequence of one or more dictionary words.
1011
* You may assume the dictionary does not contain duplicate words.
@@ -22,10 +23,8 @@ The wordDict parameter had been changed to a list of strings (instead of a set o
2223

2324
public class _139 {
2425

25-
public static class PureDPSolution {
26-
/**
27-
* This beats 70.10% submissions.
28-
*/
26+
public static class Solution1 {
27+
/**this beats 70.46% submission. */
2928
public boolean wordBreak(String s, List<String> wordDict) {
3029
int n = s.length();
3130
boolean[] dp = new boolean[n + 1];
@@ -42,9 +41,10 @@ public boolean wordBreak(String s, List<String> wordDict) {
4241
}
4342
}
4443

45-
public static class ModifiedDPAndPruningSolution {
44+
public static class Solution2 {
4645
/**
47-
* This beats 86.09% submissions.
46+
* Added pruning.
47+
* this beats 89.91% submissions.
4848
*/
4949
public boolean wordBreak(String s, List<String> wordDict) {
5050
int maxLen = Integer.MIN_VALUE;
@@ -70,9 +70,10 @@ public boolean wordBreak(String s, List<String> wordDict) {
7070
}
7171
}
7272

73-
public static class DPAndPruningSolution {
73+
public static class Solution3 {
7474
/**
75-
* This beats 97.08% submissions.
75+
* Added pruning, plus start from the end to check.
76+
* This beats 95.20% submissions.
7677
*/
7778
public boolean wordBreak(String s, Set<String> wordDict) {
7879
int maxLen = Integer.MIN_VALUE;

src/test/java/com/fishercoder/_139Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
import static junit.framework.Assert.assertEquals;
1212

1313
public class _139Test {
14-
private static _139.ModifiedDPAndPruningSolution modifiedDpAndPruningSolution;
14+
private static _139.Solution2 solution2;
1515
private static String s;
1616
private static List<String> wordDict;
1717

1818
@BeforeClass
1919
public static void setup() {
20-
modifiedDpAndPruningSolution = new _139.ModifiedDPAndPruningSolution();
20+
solution2 = new _139.Solution2();
2121
}
2222

2323
@Test
2424
public void test1() {
2525
s = "leetcode";
2626
wordDict = new ArrayList<>(Arrays.asList("leet", "code"));
27-
assertEquals(true, modifiedDpAndPruningSolution.wordBreak(s, wordDict));
27+
assertEquals(true, solution2.wordBreak(s, wordDict));
2828
}
2929
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy