Monday, March 13, 2023

Coding Challenge #20

 // based on Daily Coding Challenge #22
// given a dictionary (array of words) and a word (a string) without spaces between words, return all the dictionary words found in the string
// example:
// dictionary: ['cat', 'dog', 'cathode']
// string: cathodedog
// result: ['cat', 'cathode', 'dog']
function getAllStrs(dictionary, str){
    var result = [];
    for(var xint = 0, len = str.length;xint < len;xint++){
        var tmp = str.substr(xint);
        for(var yint = 0, len2 = dictionary.length;yint < len2;yint++){
            if(tmp.indexOf(dictionary[yint]) == 0) result.push(dictionary[yint])
        }
    }
    return result;
}

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...