javascript solution


  • 0
    J
     * @param {string} s
     * @param {string[]} words
     * @return {number[]}
     */
    var findSubstring = function(s, words) {
        var res = [];
        if(s===null || words===null) return res;
        var lenw = words[0].length;
        var map = {};
        for(let i=0;i<words.length;i++){
            if(map[words[i]]===undefined){
                map[words[i]]=1;
            }else
            map[words[i]]++;
        }
        console.log(map);
        for(let i=0;i+words.length*lenw<=s.length;i++){
            var c = s.slice(i,i+lenw);
            if(map[c]!==undefined){
            var tmp = {};
            for(let k in map){
                tmp[k] = map[k];
            }
            var flg = true;
            for(let j=0;j<words.length;j++){
                var st = s.slice(i+j*lenw,i+(j+1)*lenw);
                if(tmp[st]===undefined || tmp[st]===0) {
                    flg = false;
                    break;
                }else
                tmp[st]--;
            }
            if(flg){
                for(let k in tmp){
                    if(tmp[k]!==0){
                        flg = false;
                        break;
                    }
                }
            }
            if(flg){
                res.push(i);
            }
            }
        }
        return res;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.