Word Break

来源:互联网 发布:ads软件 编辑:程序博客网 时间:2024/05/04 01:01
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 str: wordDict) {                int len = str.length();                if (i + len > s.length()) {                    continue;                }                int end = i + len;                if (s.substring(i, end).equals(str)) {                    res[end] = true;                }            }        }        return res[s.length()];    }}

0 0