Easy understanding JavaScript solution


  • 0
    L
    /**
     * @param {string} str
     * @return {boolean}
     */
    var repeatedSubstringPattern = function(str) {
        var len = str.length;
        var half = Math.floor(len / 2);
    
        for(var i = 1; i <= half; i ++){
            if(len % i !== 0){
                continue;
            }
            var cur = str.substr(0, i);
            var flag = true;
            for(var j = i; j <= len - i; j += i){
                if(str.substr(j, i) !== cur){
                    flag = false;
                    break;
                }
            }
            if(flag){
                return true;
            }
        }
        return false;
    };
    

Log in to reply
 

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