139. Word Break(js)

139. Word Break

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.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
             Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false题意:能否在wordDict中抽出几个单词(可以重复抽取)拼凑成s代码如下:
/**
 * @param {string} s
 * @param {string[]} wordDict
 * @return {boolean}
 */
var wordBreak = function(s, wordDict) {
    var tmp=[];
    for(var i=0;i<s.length+1;i++){
        if(i===0) tmp[i]=true;
        else tmp[i]=false;
    }

    for(var i=1;i<=s.length;i++){
        for(var j=0;j<i;j++){
            if(tmp[j] && wordDict.includes(s.substring(j,i))){
                tmp[i]=true;
                break;
            }

        }
    }
    return tmp[s.length];
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10986765.html

时间: 2024-10-10 03:20:00

139. Word Break(js)的相关文章

127. Word Ladder(js)

127. Word Ladder Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time. Each transformed word must exi

【leetcode】Word Break (middle)

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

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】Word Break(python)

思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符開始找连续的子串,可是这时与第一个就稍有不同.比方说word='ab', dict={ 'a', ab', ...},在找到a后,接下来处理的是b.我们发现b不在dict中,可是我们发现b能够和a结合,形成ab,而ab在dict中.所以这里的每一个子串就能够有三种选择.要么自己单独作为

UVA 417 - Word Index(数论)

题意:417 - Word Index 题意:每个字符串按题目中那样去映射成一个数字,输入字符串,输出数字 思路:这题还是比较水的,由于一共只有83000多个数字,所以对应一个个数字去映射就可以了,注意字符串进位的情况处理即可 代码: #include <stdio.h> #include <string.h> #include <map> #include <string> using namespace std; char str[10]; map<

给数组添加一个根据指定下标删除元素的方法、得到0-100的随机数不重复(js)、得到外联样式的css样式值

/** *删除数组指定下标或指定对象 */ Array.prototype.remove=function(obj){ for(var i =0;i <this.length;i++){ var temp = this[i]; if(!isNaN(obj)){ temp=i; } if(temp == obj){ for(var j = i;j <this.length;j++){ this[j]=this[j+1]; } this.length = this.length-1; } } }

JavaScript(JS)之简单介绍

JavaScript(JS)之简单介绍 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Netscape(网景)接收Nombas的理念,(Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript 微软随后模仿在其IE3.0

百度静态资源(JS)公共库

     例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps.bdimg.com/libs/classlist/2014.01.31/classList.min.js cookies.js http://apps.bdimg.com/libs/Cookies.js/0.4.0/cookies.min.js dojo http://apps.bdimg.com/l

前台强大的图表(js)

http://www.jqplot.com/ 网址,自己下载下例子看一下就明白了. 这个是我之前做的项目中的例子(仅是部分代码): function publicMethod(){                         var plot1 = $.jqplot(chart, [currYear], {                 seriesColors: ["rgb(23, 108, 238)"],                 title: titles,