How Many Substrings?

  • + 0 comments

    solution in JS:

    getting timeout error because the time complexity is O(n*m*k), help me to reduce the complexity:

    function countSubstrings(s, queries) {
        // Write your code here
        let left;
        let right;
        queries.forEach((query, index) => {
            let mainSubString = [];
            left = query[0];
            right = query[1];
            const subString = s.slice(left, right+1);
            const sstrLength = subString.length;
            for(let i=0; i<sstrLength; i++){
            for (let j = i + 1; j <= sstrLength; j++) {
                if(!mainSubString.includes(
                    subString.slice(i, j))){
                mainSubString.push(subString.slice(i, j))
                }
    
            }
            }
            console.log(mainSubString.length)  
        })
    
    }