首页 > 其他分享 >139.WordBreak

139.WordBreak

时间:2022-11-20 16:38:12浏览次数:34  
标签:dictionary WordBreak length boolean words wordDict 139 true

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. You may assume the dictionary does not contain duplicate words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

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

 

class Solution {

    public boolean wordBreak(String s, List wordDict) {

       boolean[] f = new boolean[s.length() + 1];      

       f[0] = true;       

       for(int i = 0; i <= s.length(); i++){

          for(int j = i; j < s.length(); j++){

              if(f[i] && wordDict.contains(s.substring(i, j + 1))){

                 f[j + 1] = true;         

             }

          }

       }

       return f[s.length()];

   }

}

标签:dictionary,WordBreak,length,boolean,words,wordDict,139,true
From: https://www.cnblogs.com/MarkLeeBYR/p/16908779.html

相关文章

  • [Go 夜读 第 139 期] Go 语言 Excelize 开源基础库介绍
    Excelize是Go语言编写的用于操作电子表格文档的基础库,支持XLAM/XLSM/XLSX/XLTM/XLTX等多种文档格式,高度兼容带有样式、图片(表)、透视表、切片器等复杂组件......
  • P1396 营救
    ​​传送门​​思路:克鲁斯卡尔最小生成树,在加入边的时候判断s与t是否已经连通即可。#include<bits/stdc++.h>usingnamespacestd;#definelllonglongconstintinf=0x3......
  • Codeforces - 1391C - Cyclic Permutations(思维 + 组合数学 + 数论 + 图论、*1500)
    1391C-CyclicPermutations(⇔源地址)目录1391C-CyclicPermutations(⇔源地址)tag题意思路AC代码错误次数:0tag⇔思维、⇔组合数学、⇔数论、⇔......
  • CF1394D
    所谓单调递增或单调递减,其实只用看成一条有向的链,\(b\)大的点指向\(b\)小的点即可。所以对于边\((u,v)\),假设\(b_u\neb_v\),那么这条边的方向确定。一个点对答案的......
  • CF1394C
    首先,我们关注一下“相似”是什么意思:它等价于,两个字符串中B和N的数量分别相同。显然地我们可以发现,每次操作,相当于给字符串加或减一个B或N或BN。把每个字符串中......
  • 【CF1396E】Distance Matching(构造)
    题意:给一棵\(n\)个点的树,保证\(n\)为偶数,你需要将这\(n\)个点两两配对,使得每对点的距离和恰好为\(k\)。判断无解或输出方案。\(n\leq10^5,k\leqn^2\)。题解:首......
  • react实战笔记139:使用RTK构建store1
    创建切片切片对象 导出数据 单个reduccer 多个reducer......
  • HDU 1394 Minimum Inversion Number
    题目链接:​​传送门​​求出原数组的逆序对算把一个数从对头拿到队尾的过程中产生的贡献诶我好像昨天做过这个题#include<iostream>#include<cstdio>#include<cstring......
  • ARC139F Many Xor Optimization Problems
    题意:给定\(n,m\),求\(n\)个\([0,2^m)\)的数的最大异或和的和。瞎扯:考虑线性基,考虑消元后的,显然唯一,最大异或和为基内所有数的异或和。考虑大小为\(k\)的基方案数为......
  • Windows使用Docker出现exit 139错误
    使用DockerDesktop启动一个镜像以后,发现直接EXITED(139)这时候用dockerlogcontainerId也获取不到任何日志问题缘由找了比较多的资料哈,Centos6下对Docker支持确实......