Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean wordBreak(String s, List<String> wordDict) {
- Set<String> allWords=new HashSet<>(wordDict);
- int n=wordDict.size();
- int m=s.length();
- boolean [] dp=new boolean[m+1];
- dp[0]=true;
- for(int i=1;i<=m;++i){
- for(int j=0;j<i;++j){
- if(dp[j] && allWords.contains(s.substring(j,i))){
- dp[i]=true;
- break;
- }
- }
- }
- return dp[m];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement