【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given

s = "catsanddog",

dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

【题意】

相比Word Break,这次不仅要判断字符串能否被分解,还要把所有的分解情况给找出来。这样的话,我在Word Break用过的暴力法就可以很容易得改成这道题。

==================== 暴力法、穷尽法 ====================

【Java代码】

class Solution {
    List<String> ret;

    public List<String>  wordBreak(String s, Set<String> dict) {
        String[] all = dict.toArray(new String[0]);

        ret = new ArrayList<String>();

        //如果s中有字母没在dict出现过,那么结果肯定为false
        //这段代码的作用就是防止暴力法超时,有点投机的做法,我在Word Break解题报告中已讲过
        for (int i = 0; i < s.length(); i++) {
            boolean flag = false;
            for (int j = 0; j < all.length; j++) {
                if (all[j].indexOf(s.charAt(i)) > -1) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return ret;
            }
        }

        nextWord(0, s, all, "");
        return ret;
    }

    void nextWord(int pos, String s, String[] all, String sent) {
        if (pos == s.length()) {
            ret.add(sent.trim());
        }

        for (int i = 0; i < all.length; i++) {
            if (s.indexOf(all[i], pos) == pos) {
                String str = sent;
                str += all[i] + " ";
                nextWord(pos + all[i].length(), s, all, str);
            }
        }
    }
}
时间: 2024-11-08 20:24:14

【LeetCode】Word Break II 解题报告的相关文章

LeetCode: Word Break II 解题报告

Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat",

[LeetCode] Word Break II 解题思路

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

[LeetCode]Word Break,解题报告

目录 目录 前言 题目 DFS 动态规划 前言 过完年回来状态一直就是懒懒散散的,之前想写的年终总结也一直再拖沓.所以,从今天起找回之前的状态,每日的工作计划没完成就不能休息.今天的任务中有两道LeetCode题目,其中一道动态规划的题目稍微难一点,这里记录一下解题过程. 题目 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequen

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "c

LeetCode: Word Break II [140]

[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cat

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

[Leetcode] word break ii拆分词语

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens ="catsanddog",dict =["cat", "cats",