String.prototype.mostLongest = function() { var str = this, strLenth = str.length, longStr = [], maxNum = 1, maxLength = strLenth, arr = [], count = function(num) { for ( var i = strLenth - num + 1; i > -1 ; i--) { var temp = str.substr(i, num); arr[temp] = ~~arr[temp] + 1; if (arr[temp] > maxNum) { maxNum = arr[temp]; longStr = []; longStr.push(temp); maxLength = num; } else if (arr[temp] == maxNum) { if (num > maxLength) { maxNum = arr[temp]; longStr = []; longStr.push(temp); maxLength = num; }else if(num === maxLength){ longStr.push(temp); } } } num > 2 && count(num - 1); }; count(strLenth); return longStr; } |