Search code snippets, questions, articles... Chrome Extension

Valid Dictonary String - Bloomberg - Port Interactive Analytics - Full Stack - 11/8/21

// Start typing here// Start typing here

// Given an input of a dictionary of words and an input string that does not contain spaces, write a method that returns a string with spaces inserted accordingly.

Input: "bloombergisfun", ["bloom","bloomberg","is","fun"]
Output: "bloomberg is fun"

// Returns the shorter matching string
"bloombergisfun", ["bloom",  "bloomberg","is","fun"]
"bloom berg is fun"

function spacedString(str, dictionary) {
    let currentStr = ''
    let words = [];
    
    for(let i = 0; i < str.length; i++) {
        currentStr ++ str[0]
        
        if (dictionary.has(currentStr)) {
            words.push(currentStr);
            currentStr = '';
        } 
    }
    
    return words.join(' ');
}
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
Was this helpful?
0 Comments
Join Devsheet Ask a Question