#187 aboutl the "10-letter-long sequences occur more than once" question. I got a Time Limit Exceeded


  • 0
    H
    /**
     * @param {string} s
     * @return {string[]}
     */
    
    var findRepeatedDnaSequences = function(s) {
        var str = '';
        var arr = [];
        if(s.length < 11) return[];
        for(var i=0;i<=s.length-10;++i) {
            str = s.substring(i,i+10);
            if(s.indexOf(str, i+1) != -1) {
                arr.push(str);
            }
        }
        return arr;
    };
    

    This is my code, I tested several times and the results were ok.


    But when I submit this code, it got Time Limit Exceeded.


    I have no idea, please help me!
    Thanks!


  • 0
    S

    Same here

    var findRepeatedDnaSequences = function(s) {
        
        var len = s.length;
        var res = {};
        
        for(var i = 0; i <= len-10; i++){
            substr = s.substring(i, i+10);
            if(s.indexOf(substr) !== i && !res[substr]){
                res[substr] = true;    
            }
        }
        return Object.keys(res);
    };
    

  • 0
    S

    I think substring needs to be encoded for comparison. like as follows:
    https://github.com/paopao2/leetcode-js/blob/master/Repeated DNA Sequences.js


Log in to reply
 

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