7ms Optimized DP Solution Beats 96.44% (Java)


  • 1
    Q

    The trick for optimization is that the words in the dict have limited length. In real life, English words are limited in length as well. So j can stop once it has tried the max length. It optimizes the algorithm from O(n^2) to O(n * maxLen) where maxLen is the maximum length of the words in dict.

    // Time Complexity (brute force): O(2^n-1) because there are n - 1 possible cuts, and there are two ways for each cut (choose the cut or not)
    // Time complexity (dp): O(n*len) where len is the max len of the word in dict
    // Space complexity (dp): O(n)
    public boolean wordBreak(String s, List<String> dict) {
        if (s == null || dict == null) {
            return false;
        }
        Set<String> set = new HashSet<>(dict);
        int len = s.length();
        boolean[] dp = new boolean[len + 1];
        dp[0] = true;
    
        int maxLen = Integer.MIN_VALUE;
        for (String word : set) {
            maxLen = Math.max(maxLen, word.length());
        }
    
        for (int i = 1; i < dp.length; i++) {
            for (int j = i; j >= 1 && j >= i - maxLen + 1; j--) {
                if (set.contains(s.substring(j - 1, i)) && dp[j - 1]) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[len];
    }

Log in to reply
 

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