Advertisement
thewitchking

Untitled

Mar 25th, 2025
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. class Solution {
  2.     public boolean wordBreak(String s, List<String> wordDict) {
  3.        
  4.         Set<String> allWords=new HashSet<>(wordDict);
  5.         int n=wordDict.size();
  6.         int m=s.length();
  7.         boolean [] dp=new boolean[m+1];
  8.         dp[0]=true;
  9.         for(int i=1;i<=m;++i){
  10.             for(int j=0;j<i;++j){
  11.                 if(dp[j] && allWords.contains(s.substring(j,i))){
  12.                     dp[i]=true;
  13.                     break;
  14.                 }
  15.             }
  16.         }
  17.         return dp[m];
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement