How Many Substrings?

Sort by

recency

|

70 Discussions

|

  • + 0 comments

    I am having memory abort issues for some test cases

  • + 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)  
        })
    
    }
    
  • + 0 comments

    Here is my solution in java, C++, Csharp HackerRank How Many Substrings? Solution

  • + 0 comments

    Here are the solution of How Many Substrings? Click Here

  • + 1 comment

    Sliding window would not help, already saw the solution, maybe it's a math test, but unfortunately I'm not a genius, if you can refactor my code, please help me.. the code only pass some case.

    int counting(string s, int length){
        int re=0;
        unordered_map<string, bool>mp;
        for(int i=0;i<length;i++){
            for(int j=1;j<=length-i;j++){
                string sub = s.substr(i,j);
                if(!mp[sub]){
                    mp[sub]=true;
                    re++;
                }
            }
        }    
        
        return re;
    }
    vector<int> countSubstrings(string s, vector<vector<int>> queries) {
        
        int length=queries.size();
        vector<int> re;
        
        for(int i=0;i<length;i++){
            int left = queries[i][0];
            int right = (left > 0) ? queries[i][1] : queries[i][1]+1;
            int length = queries[i][1]-queries[i][0]+1;
            int count = counting(s.substr(left, right), length);
            re.push_back(count);
        }
        return re;
    }