Day49.算法训练

139. 单词拆分

class Solution {
    public boolean wordBreak(String s, List wordDict) {
        Set wordDicts = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;

        for (int i = 1; i < s.length() + 1; i++) {
            for (int j = 0; j < i; j++) {
                System.out.println(s.substring(j, i));
                if (dp[j] && wordDicts.contains(s.substring(j, i))) {
                    dp[i] = true;
                }
            }
        }

        return dp[s.length()];
    }
}

你可能感兴趣的:(算法)