Problem

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.

Example

Given s = "SodhanaLibrary", dict = ["Sodhana", "Library"].
Return true because "SodhanaLibrary" can be segmented as "Sodhana Library".

JavaScript Code

function wordBreak(s, dict) {
    var t = [];
    for(var i=0; i<=s.length; i++){
        t[i] = false;
    }
    t[0] = true; //set first to be true, why?
    //Because we need initial state

    console.log(t);
    for(var i=0; i<s.length; i++){
        //should continue from match position
        if(!t[i]) 
            continue;

        for(var a in dict){
            var len = dict[a].length;
            var end = i + len;
            if(end > s.length)
                continue;

            if(t[end]) continue;

            if(s.substring(i, end) == dict[a]){
                t[end] = true;
            }
        }
        console.log(t);
    }

    return t[s.length];
}
var s = "sodhanalibrary";
var dict = ["sodhana", "library"];
console.log(wordBreak(s,dict));

4 comments:

  1. Hello friends! Grab a cool tool that will help you figure out how to use the passive voice in text works, and also allow you to improve their quality and save a lot of time! The tool finds errors related to the passive voice, points to them and helps to correct them. You will also receive useful tips and tricks that will help you in the future when writing various papers!

    ReplyDelete
  2. Hello friends! I want to share with you a cool tool that will help you correctly place punctuation marks, and check if you have already placed the existing ones correctly! The tool is simple and easy to use and will save you a lot of time! All you have to do is add text to the tool and wait for the validation to complete. After that, you can quickly and easily correct errors. Good luck friends!

    ReplyDelete
  3. If you are into essay writing, then I recommend you try the free passive voice checker and corrector app . Using the active verb converter provides you with many features and options to help you make your piece of text look amazing and provide a great user experience. Online grammar checker offers many additional benefits that you cannot achieve with manual services with any writing services. The main reason is that online services are based on the fusion of modern technology and the knowledge of expert linguists. In addition, the online voice check service will help you improve your vocabulary by using the most suitable word in the text to make it the most professional.

    ReplyDelete
  4. Hey! Our passive voice corrector is very popular among students and writers. With it, you can identify all grammatical errors in your text as well as correct the passive voice. Our passive voice checker online free software ​has already been tested by thousands of users, is intuitive and most importantly absolutely free. Check your text right now

    ReplyDelete

Blogroll

Popular Posts