[LC] 139. Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
             Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false

Time: O(N^3)
Space: O(N)

 1 class Solution:
 2     def wordBreak(self, s: str, wordDict: List[str]) -> bool:
 3         can_break = [False] * (len(s) + 1)
 4         
 5         for i in range(1, len(s) + 1):
 6             if s[:i] in wordDict:
 7                 can_break[i] = True
 8                 continue
 9             for j in range(1, i):
10                 if can_break[j] and s[j: i] in wordDict:
11                     can_break[i] = True
12                     break
13         return can_break[len(s)]
14                 

 

DFS

public class Solution {
  public boolean canBreak(String input, String[] dict) {
    Set<String> set = new HashSet<>(Arrays.asList(dict));
    return helper(input, set, 0);
  }

  private boolean helper(String input, Set<String> set, int index) {
    if (index == input.length()) {
      return true;
    }
    for (int i = index + 1; i <= input.length(); i++) {
      if (set.contains(input.substring(index, i)) && helper(input, set, i)) {
          return true;
      }
    }      
    return false;
  }
}

 

public class Solution {
    /*
     * @param s: A string
     * @param dict: A dictionary of words dict
     * @return: A boolean
     */
    public boolean wordBreak(String s, Set<String> dict) {
        return helper(s, dict, 0);
    }
    
    private boolean helper(String s, Set<String> dict, int index) {
        if (index == s.length()) {
            return true;
        }
        for (String str: dict) {
            int len = str.length();
            if (index + len > s.length()) {
                continue;
            }
            if (!s.substring(index, index + len).equals(str)) {
                continue;
            }
            if (helper(s, dict, index + len)) {
                return true;
            }
        }
        return false;
    }
}

 

 

 

DP

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] inDict = new boolean[s.length() + 1];
        Set<String> set = new HashSet<>();
        for (String word: wordDict) {
            set.add(word);
        }
        inDict[0] = true;
        for (int i = 1; i < inDict.length; i++) {
            // substring(i) is beginIndex
            if (set.contains(s.substring(0, i))) {
                inDict[i] = true;
                continue;
            }
            for (int j = 0; j < i; j++) {
                if (inDict[j] && set.contains(s.substring(j, i))) {
                    inDict[i] = true;
                    break;
                }
            }
        }
        return inDict[inDict.length - 1];
     }
}
posted @ 2019-10-14 03:16  xuan_abc  阅读(137)  评论(0编辑  收藏  举报