word break
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 // dfs class Solution { public boolean wordBreak(String s, List<String> wordDict) { if(s == null || wordDict == null) return false; if(s.equals("")) return true; for(int i = 0; i < s.length(); i++){ String substr = s.substring(0, i + 1); if(wordDict.contains(substr)){ if(wordBreak(s.substring(i + 1), wordDict)){ return true; } wordDict.remove(substr); } } return false; } } // dp
// Time complexity : O(n^2)
// Two loops are their to fill dp array.
//Space complexity : O(n). Length of pp array is n+1
class Solution { public boolean wordBreak(String s, List<String> wordDict) { HashSet<String> set = new HashSet<>(); for(String word : wordDict){ set.add(word); } boolean[] dp = new boolean[s.length() + 1]; dp[0] = true; for(int i = 1; i < dp.length; i++){ for(int j = 0; j < i; j++){ if(dp[j] && set.contains(s.substring(j, i))){ dp[i] = true; break; } } } return dp[s.length()]; } } 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
posted on 2018-09-08 07:07 猪猪🐷 阅读(153) 评论(0) 收藏 举报
                    
                
                
            
        
浙公网安备 33010602011771号