139 Word Break

139 Word Break
Recursion + memorization  要弄懂
再找道 Recursion + memorization 的题做做

https://www.youtube.com/watch?v=ptlwluzeC1I&t=419s

https://www.youtube.com/watch?v=RPeTFTKwjps


https://zxi.mytechroad.com/blog/leetcode/leetcode-139-word-break/

https://github.com/tongzhang1994/Facebook-Interview-Coding/blob/master/139.%20Word%20Break.java

https://leetcode.com/problems/word-break/solution/
不会。。 画图
Sol 1:  Recursion + memorization   PASSED most cases, TLE in some cases 

Improvement : maybe we can set true = 1 
class Solution {
      
    public boolean wordBreak(String s, List<String> wordDict) {
      Set<String> dict = new HashSet(wordDict);
      // Use Boolean instead of boolean. Map can only contain objects and boolean is a primitive type not an object. Boolean is object wrapper of boolean.
      HashMap<String, Boolean> memo = new HashMap<>(); 
      
      return wordBreak(s, dict, memo);
    }
    
    private boolean wordBreak(String s, Set<String> dict, HashMap<String, Boolean> memo){
      // if s is in the memo
      if(memo.containsKey(s)){
        return memo.get(s);
      }
      
      // if s is in the dict 
      if(dict.contains(s)){
        memo.put(s, true);
        return true;
      }
      
      
      // if s is not in the memo and s is not in the dict, we need to cut it 
      // and solve it recursively 
      for(int i = 0; i < s.length(); i++){
        String left = s.substring(0, i);
        String right = s.substring(i);
        
        if(wordBreak(left, dict, memo) && dict.contains(right)){
          memo.put(s, true);
          return true;
        }
      }
      return false;
    }
}





Sol2: dp 

1 means true. Default int[] is 0 , 0 means false 
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
      HashSet<String> set = new HashSet(wordDict);
      int n = s.length();
      s = " " + s;
      int[] f = new int[n + 1];
      f[0] = 1;
      
      for(int i = 1; i <= n; i++){
        for(int j = 0; j < i; j++){
          if(f[j] == 1){
            String right = s.substring(j + 1, i+1); // exclusive i+1
            if(set.contains(right)){
              f[i] = 1;
              break; // once we have true, break here , no need to continue under the current I 
            }
          }
        }
      }
      return f[n] == 1;
    }
}

dp builds up from bottom 

s= “”+ leetcode
f=[0     00000000]

Initalize 

f[0]   —————————breakable 
“”   breakable 



f[1]  —————————not breakable 
“”+ l 
“” Breakable , l is not in the dict
f[1] remains 0, which means false 



f[2]  —————————not breakable 
“”+ le
“” Breakable , le is not in the dict , 
“” + l is not breakable , f[1]


f[3]  —————————not breakable 
“”+ lee
“” Breakable  f[0], lee is not in the dict , 
“” + l is not breakable f[1] 
“”+ le is not brekable f[2]


f[4]  ————————— breakable 
“”+ leet

“” Breakable f[0] , leet is  in the dict 




f[5]  —————————not breakable 
“”+ leetc
“” Breakable , leetc is not in the dict 
“”+l, not breakable f[1] 
“”+ le is not brekable f[2]
“” + lee not breakable f[3]
“” + leet breakable f[4], c is not in the dict 



f[6]  —————————not breakable 
“”+ leetco
“” Breakable , leetco is not in the dict 
“”+l, not breakable f[1] 
“”+ le is not brekable f[2]
“” + lee not breakable f[3]
“” + leet breakable f[4], co is not in the dict 
“” + leetc not breakable f[5]


f[7]  —————————not breakable 
“”+ leetcod
“” Breakable , leetcod is not in the dict 
“”+l, not breakable f[1] 
“”+ le is not brekable f[2]
“” + lee not breakable f[3]
“” + leet breakable f[4], cod is not in the dict 
“” + leetc not breakable f[5]
“” + leetco not breakable f[6]


f[8]  ————————— breakable 
“”+ leetcode
“” breakable , leetcod is not in the dict 
“”+l, not breakable f[1] 
“”+ le is not brekable f[2]
“” + lee not breakable f[3]
“” + leet breakable f[4], code is  in the dict 

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9455076.html
今日推荐