Skip to content

Create 139 Word Break #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 139 Word Break
Java Word Break DP solution.
  • Loading branch information
Megha1209 authored Sep 30, 2020
commit 4acb4f910dae983dac0004cd3d0b09745ff711d7
19 changes: 19 additions & 0 deletions src/com/blankj/medium/139 Word Break
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public boolean wordBreak(String s, List<String> wordDict) {
HashSet<String> words=new HashSet(wordDict);
int n=s.length();
boolean[] dp=new boolean[n+1];

int len=0;
for(String word: wordDict) len=Math.max(len,word.length());

dp[0]=true;
for(int i=0;i<n;i++){
if(!dp[i]) continue;

for(int l=1;i+l<=n && l <= len ; l++){
if(words.contains(s.substring(i,i+l))) dp[i+1]=true;
}
}

return dp[n];
}