leetcode Word Break python

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 = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

python code:

class Solution:
# @param s, a string
# @param wordDict, a set<string>
# @return a boolean
def wordBreak(self, s, wordDict):
  if not s or not wordDict:    #边界条件处理
    return False
  d=[0]              #这个list用来记录前多少个字符可以匹配到,如[0,2,4,6],表示前0,2,4,6个字符构成的子串可以成功匹配
  for i in range(1,len(wordDict)+1):  #检验s,从第一个字符到第一个字符直至第一个字符到最后一个字符的子串是否匹配
    for j in range(len(d)-1,-1,-1):  #s的前i个字符构成的子串怎样才算匹配?上一个匹配的位置到i构成的子串加上前面某个能匹配的子串后可以

                     #  在wordDict中找到
      if s[d[j]:i] in wordDict:
        d.append(i)
        break
  return len(s)==d[-1]           #如果长为len(s)的子串能匹配成功,那么return True

时间: 2024-10-07 01:30:09

leetcode Word Break python的相关文章

[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

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 = ["leet", "code"]. Return true because

LeetCode: Word Break [139]

[题目] 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 = ["leet", "code"]. Return true be

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

Given a string s and a dictionary of words dict, determine ifs can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true because

Leetcode:Word Break 字符串分解为单词

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 = ["leet", "code"]. Return t

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 I

Word Break 使用DFS遍历当然是可以的,尝试使用DP应该更加简洁,发现leetcode讨论确实是一个非常不错的资源. 下面是我的改进代码 1 public class Solution { 2 public boolean wordBreak(String s, Set<String> wordDict) { 3 if(s==null||s.length()==0) return false; 4 //flag[i] represents if s[0,...,i] can be f

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