Word Break II 解答

Question

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"].

Solution

这题的基本思路是用recursion,每次都只研究第一刀切在哪儿,然后对剩下的substring做递归。但是我们发现其实可以借助DP的思想,存下来部分中间结果值。这种思想也叫 Memorized Recursion

Memorized recursion并不是一个很好的方法,但是当遇到time limit exceed时可以想到它来拿空间换时间

 1 class Solution:
 2     # @param s, a string
 3     # @param dict, a set of string
 4     # @return a list of strings
 5     def wordBreak(self, s, dict):
 6         self.dict = dict
 7         # cache stores tmp results: key: substring value: list of results
 8         self.cache = {}
 9         return self.break_helper(s)
10
11     def break_helper(self, s):
12         combs = []
13         if s in self.cache:
14             return self.cache[s]
15         if len(s) == 0:
16             return []
17
18         for i in range(len(s)):
19             if s[:i+1] in self.dict:
20                 if i == len(s) - 1:
21                     combs.append(s[:i+1])
22                 else:
23                     sub_combs = self.break_helper(s[i+1:])
24                     for sub_comb in sub_combs:
25                         combs.append(s[:i+1] + ‘ ‘ + sub_comb)
26
27         self.cache[s] = combs
28         return combs
时间: 2024-08-09 19:50:39

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",

[leecode]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, givens = "catsanddog",dict = ["cat", &q

Word Break && Word Break II

Word Break && Word Break II Word Break 题目: 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", dict = ["

Leetcode dfs Word Break II

Word Break II Total Accepted: 15138 Total Submissions: 92228My Submissions 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 ex

【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"

Word Break II leetcode java

题目: 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

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 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

Word Break II 求把字符串拆分为字典里的单词的所有方案 @LeetCode

这道题类似  Word Break 判断是否能把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不仅仅是是否能拆分,而是要求出所有的拆分方案.因此用递归. 但是直接递归做会超时,原因是LeetCode里有几个很长但是无法拆分的情况,所以就先跑一遍Word Break,先判断能否拆分,然后再进行拆分. 递归思路就是,逐一尝试字典里的每一个单词,看看哪一个单词和S的开头部分匹配,如果匹配则递归处理S的除了开头部分,直到S为空,说明可以匹配. public class Solution