public boolean wordBreak(String s, Set<String> wordDict) {
        boolean flag[] = new boolean[s.length()];
        for (int i = 0; i < s.length(); ++i) {
            flag[i] = flag[i] || wordDict.contains(s.substring(0, i + 1));
            for (int j = i + 1; j < s.length(); ++j) {
                flag[j] = flag[j] || (flag[i] && wordDict.contains(s.substring(i + 1, j + 1)));
            }
        }
        return flag[s.length() - 1];
    }