139. 单词拆分

    public boolean wordBreak(String s, List<String> wordDict) {
        return wordBreakSet(s, new HashSet<>(wordDict));
    }

    private boolean wordBreakSet(String s, HashSet<String> wordDict) {
        if (s.isEmpty()) {
            return true;
        }
        for (int i = 0; i < s.length(); i++) {
            String before = s.substring(0, i);
            String after = s.substring(i);
            if (wordDict.contains(after) && wordBreakSet(before, wordDict)) {
                return true;
            }
        }
        return false;
    }
  • 递归+记忆 accept,时间复杂度:O(N^2), 空间复杂度O(N):递归的深度
public boolean wordBreak(String s, List<String> wordDict) {
        return wordBreakSet(s, new HashSet<>(wordDict), new HashSet<>());
    }

    private boolean wordBreakSet(String s, HashSet<String> wordDict, HashSet<String> memory) {
        if (s.isEmpty()) {
            return true;
        }
        if (memory.contains(s)) return false; //记忆
        for (int i = 0; i < s.length(); i++) {
            String before = s.substring(0, i);
            String after = s.substring(i);
            if (wordDict.contains(after) && wordBreakSet(before, wordDict, memory)) {
                return true;
            }
        }
        memory.add(s); //记忆
        return false;
    }
  • 单纯的BFS,超时
public boolean wordBreak(String s, List<String> wordDict) {
        LinkedList<String> queue = new LinkedList<>();
        queue.add(s);
        while (!queue.isEmpty()) {
            String seg = queue.poll();
            for (String word : wordDict) {
                if (seg.startsWith(word)) {
                    String otherSeg = seg.substring(word.length());
                    if (otherSeg.isEmpty()) {
                        return true;
                    }
                    queue.add(otherSeg);
                }
            }
        }
        return false;
    }
  • BFS + 记忆,accept
public boolean wordBreak(String s, List<String> wordDict) {
        LinkedList<String> queue = new LinkedList<>();
        queue.add(s);
        Set<String> memory = new HashSet<>(); //记忆
        while (!queue.isEmpty()) {
            String seg = queue.poll();
            if (!memory.contains(seg)) { //记忆
                for (String word : wordDict) {
                    if (seg.startsWith(word)) {
                        String otherSeg = seg.substring(word.length());
                        if (otherSeg.isEmpty()) {
                            return true;
                        }
                        queue.add(otherSeg);
                    }
                }
                memory.add(seg); //记忆
            }
        }
        return false;
    }

原文地址:https://www.cnblogs.com/lasclocker/p/11410026.html

时间: 2024-07-30 13:00:08

139. 单词拆分的相关文章

[LeetCode] 139. 单词拆分 ☆☆☆(动态规划 回溯)

139. 单词拆分 描述 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词.你可以假设字典中没有重复的单词.示例 1: 输入: s = "leetcode", wordDict = ["leet", "code"]输出: true解释: 返回 true 因为 "leetcode" 可以被拆分成 "

leetcode 139.单词拆分

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字典中没有重复的单词. 示例 1: 输入: s = "leetcode", wordDict = ["leet", "code"] 输出: true 解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet cod

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

leetcode139.单词拆分

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 思路分析: (1)比较容易想到的解法是,从字符串 s 开头处开始匹配,若匹配到字典里的字符串,则继续匹配剩下的字符串.这样递归下去,直到找到一个可行的序列或尝试所有的单词组合. 提交的时候发现算法超时,后来想想其实这种解法存在很大的冗余,比如同时存在 a, b, ab的字典.只需记录下字符串 s 中的序列可以被字典匹配到即可,其实就是动态规划的思想: (2)采用动

单词拆分II

#单词拆分II#给一字串s和单词的字典dict,在字串中增加空格来构建一个句子,并且所有单词都来自字典.#返回所有有可能的句子.#EXAMPLE:#给一字串lintcode,字典为["de", "ding", "co", "code", "lint"]#结果为["lint code", "lint co de"]. class Solution:    "&

leetcode 139 word break (单词拆分)

一开始的错误答案与错误思路,幻想直接遍历得出答案: 1 class Solution { 2 public: 3 bool wordBreak(string s, vector<string>& wordDict) { 4 for(int i;i<s.size();i++){ 5 int step=0; 6 for(int j;j<wordDict.size();j++){ 7 if(s.substr(i,wordDict[j].size())==wordDict[j]){

leetcode 单词拆分 II java

题目: 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分隔时可以重复使用字典中的单词. 你可以假设字典中没有重复的单词. 示例 1: 输入: s = "catsanddog" wordDict = ["cat", "cats", "and", "sand", "dog&qu

单词拆分

方法 1:暴力算法最简单的实现方法是用递归和回溯.为了找到解,我们可以检查字典单词中每一个单词的可能前缀,如果在字典中出现过,那么去掉这个前缀后剩余部分回归调用.同时,如果某次函数调用中发现整个字符串都已经被拆分且在字典中出现过了,函数就返回 true . public boolean wordBreak(String s, List<String> wordDict) { return word_Break(s, new HashSet(wordDict), 0); } public boo

【leetcode-139】【回溯超时、动态规划】单词拆分

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词.你可以假设字典中没有重复的单词.示例 1: 输入: s = "leetcode", wordDict = ["leet", "code"]输出: true解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code&qu