From 0511855de2da97015fa2c04bf9f18548046622ca Mon Sep 17 00:00:00 2001 From: yennanliu Date: Thu, 7 Mar 2024 09:21:48 +0800 Subject: [PATCH] update 139 java --- data/progress.txt | 2 +- data/to_review.txt | 18 +++++------ .../LeetCodeJava/BackTrack/WordBreak.java | 32 ++++++++++++++++++- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index 8445ddaa..5cbf2aea 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240307: 139 +20240307: 139(again) 20240306: 121,124(again),125,128,133(again) 20240305: 100,102,104,105(again) 20240304: 73,76,79(again),91,25 diff --git a/data/to_review.txt b/data/to_review.txt index 6033387a..f2c80788 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-05-01 -> ['139'] +2024-05-01 -> ['139(again)'] 2024-04-30 -> ['121,124(again),125,128,133(again)'] 2024-04-29 -> ['100,102,104,105(again)'] 2024-04-28 -> ['73,76,79(again),91,25'] @@ -6,7 +6,7 @@ 2024-04-24 -> ['39,48(again),49,53,54'] 2024-04-23 -> ['20,21,23,33(again)'] 2024-04-22 -> ['1,3,5,4,19'] -2024-04-10 -> ['139'] +2024-04-10 -> ['139(again)'] 2024-04-09 -> ['121,124(again),125,128,133(again)'] 2024-04-08 -> ['100,102,104,105(again)'] 2024-04-07 -> ['73,76,79(again),91,25'] @@ -14,25 +14,25 @@ 2024-04-03 -> ['39,48(again),49,53,54'] 2024-04-02 -> ['20,21,23,33(again)'] 2024-04-01 -> ['1,3,5,4,19'] -2024-03-28 -> ['139'] +2024-03-28 -> ['139(again)'] 2024-03-27 -> ['121,124(again),125,128,133(again)'] 2024-03-26 -> ['100,102,104,105(again)'] 2024-03-25 -> ['73,76,79(again),91,25'] 2024-03-24 -> ['55(again),56,62,70'] 2024-03-21 -> ['39,48(again),49,53,54'] -2024-03-20 -> ['139', '20,21,23,33(again)'] +2024-03-20 -> ['139(again)', '20,21,23,33(again)'] 2024-03-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19'] 2024-03-18 -> ['100,102,104,105(again)'] 2024-03-17 -> ['73,76,79(again),91,25'] 2024-03-16 -> ['55(again),56,62,70'] -2024-03-15 -> ['139'] +2024-03-15 -> ['139(again)'] 2024-03-14 -> ['121,124(again),125,128,133(again)'] 2024-03-13 -> ['100,102,104,105(again)', '39,48(again),49,53,54'] -2024-03-12 -> ['139', '73,76,79(again),91,25', '20,21,23,33(again)'] +2024-03-12 -> ['139(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-11 -> ['121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19'] -2024-03-10 -> ['139', '100,102,104,105(again)'] -2024-03-09 -> ['139', '121,124(again),125,128,133(again)', '73,76,79(again),91,25'] -2024-03-08 -> ['139', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] +2024-03-10 -> ['139(again)', '100,102,104,105(again)'] +2024-03-09 -> ['139(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25'] +2024-03-08 -> ['139(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54'] 2024-03-07 -> ['121,124(again),125,128,133(again)', '100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)'] 2024-03-06 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19'] 2024-03-05 -> ['73,76,79(again),91,25', '55(again),56,62,70', '39,48(again),49,53,54'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java b/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java index 18795f06..29a514a5 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BackTrack/WordBreak.java @@ -33,7 +33,37 @@ public class WordBreak { // } // V0 - // IDEA : BACKTRACK (modify above via GPT) + // IDEA : BFS + // https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Backtracking/word-break.py + public boolean wordBreak(String s, List wordDict) { + if (s == null || s.length() == 0 || wordDict == null || wordDict.size() == 0) { + return false; + } + + Deque q = new ArrayDeque<>(); + q.offer(0); + boolean[] visited = new boolean[s.length()]; + + while (!q.isEmpty()) { + int i = q.poll(); + if (!visited[i]) { + for (int j = i + 1; j <= s.length(); j++) { + if (wordDict.contains(s.substring(i, j))) { + if (j == s.length()) { + return true; + } + q.offer(j); + } + } + visited[i] = true; + } + } + + return false; + } + + // V0 + // IDEA : BACKTRACK (modified via GPT) public boolean wordBreak_(String s, List wordDict) { return check(s, new HashSet<>(wordDict), 0, new Boolean[s.length()]); }