DP solution with JAVA


  • 1
    R

    public boolean wordBreak(String s, List<String> wordDict) {
    if (s == null || s.length() == 0) return true;
    // dp solution
    int len = s.length();
    boolean[] dp = new boolean[len+1];
    dp[0] = true;
    for (int i = 1; i <= len; i++) {
    if(wordDict.contains(s.substring(0, i))) dp[i] = true;
    for(int j = i+1; j <= len; j++) {
    if(dp[i]== true && wordDict.contains(s.substring(i,j))) dp[j] = true;
    }
    }
    return dp[len];
    }


Log in to reply
 

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