139. Word Break

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp=new boolean[s.length()+1];
        dp[0]=true;
        for(int i=0;i<s.length();i++)
            for(String w:wordDict)
                if(i+1-w.length()>=0&&w.equals(s.substring(i+1-w.length(),i+1)))
                    dp[i+1]=dp[i+1]||dp[i+1-w.length()];
        return dp[s.length()];
    }
}

  

posted @ 2017-10-10 03:50  Weiyu Wang  阅读(118)  评论(0编辑  收藏  举报