寻找最长连续字符串

      var s = 'aaaabbbbzzzzzzzccccccc111111111';
      var reg = /(.|\n)\1*/g;
      var r = {};
      var x = s.match(reg);
      var maxLength = 0;
      var temp = [];
      var resultStr = '';
      if (x) {
        for (var i = x.length - 1; i > -1; i--) {
          if (!r[x[i][0]]) {
            r[x[i][0]] = x[i].length;
          } else {
            r[x[i][0]] = Math.max(r[x[i][0]], x[i].length);
          }
        }
      }
      for (const key in r) {
        if (r.hasOwnProperty(key)) {
          if(r[key] >= maxLength){
            maxLength = r[key];
            temp.push(key);
          }
        }
      }
      var result= temp.sort();
      for(var j = 0; j < maxLength; j ++){
        resultStr += result[0];
      }
      console.log(maxLength)
      console.log(result[0])
      console.log(resultStr)

你可能感兴趣的:(寻找最长连续字符串)