File tree Expand file tree Collapse file tree 1 file changed +3
-6
lines changed
src/main/java/leetcode/medium Expand file tree Collapse file tree 1 file changed +3
-6
lines changed Original file line number Diff line number Diff line change 44
55public class WordBreak {
66
7- boolean wordBreak (String s , java . util . List <String > wordDict ) {
7+ boolean wordBreak (String s , List <String > wordDict ) {
88
99 // Convert the dictionary to a set for O(1) lookups
1010 Set <String > wordSet = new HashSet <>(wordDict );
@@ -22,17 +22,14 @@ boolean wordBreak(String s, java.util.List<String> wordDict) {
2222 // Base case: empty string is valid
2323 dp [0 ] = true ;
2424
25- for (int i = 1 ; i <= n ; i ++) {
25+ for (int i = 1 ; i <= n ; i ++)
2626
2727 // Check prefixes of length up to maxLen
28- for (int j = i - 1 ; j >= Math .max (0 , i - maxLen ); j --) {
28+ for (int j = i - 1 ; j >= Math .max (0 , i - maxLen ); j --)
2929 if (dp [j ] && wordSet .contains (s .substring (j , i ))) {
3030 dp [i ] = true ;
3131 break ; // No need to check further prefixes
3232 }
33- }
34-
35- }
3633
3734 return dp [n ];
3835 }
You can’t perform that action at this time.
0 commit comments