Word Break

来源:互联网 发布:电脑软件安装后打不开 编辑:程序博客网 时间:2024/05/16 00:57
public class Solution {    public boolean wordBreak(String s, Set<String> wordDict) {        boolean[] res = new boolean[s.length() + 1];        res[0] = true;        for (int i = 0; i < s.length(); i++) {            if (!res[i]) {                continue;            }            for (String word: wordDict) {                int len = i + word.length();                if (len > s.length()) {                    continue;                }                if (s.substring(i, len).equals(word)) {                    res[len] = true;                }            }        }        return res[s.length()];    }}

0 0