share a java dp solution, beat 82%


  • 0
    W
    public boolean wordBreak(String s, Set<String> wordDict) {
        int len = s.length();
        int[] dp = new int[len+1];
        dp[0] = 1;
        for(int i = 1; i <= len; i++) {
            for(int j = i-1; j >= 0; j--) {
                if(dp[j] != 1) continue;
                String target = s.substring(j,i);
                if(wordDict.contains(target)) {
                    dp[i] = 1;
                    break;
                }
            }
        }
        return dp[len]==1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.