第 114 题:找出字符串中连续出现最多的字符和个数

发布于 2022-05-04 13:28:29 字数 97 浏览 1105 评论 55

'abcaakjbb' => {'a':2,'b':2}
'abbkejsbcccwqaa' => {'c':3}

注意:题目说的是连续出现,注意连续二字。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(55

夜血缘 2022-05-04 13:55:09
function find(str){
let Obj={};
	str.split('').reduce((o,p,index)=>{
		o===p?(Obj[o]?Obj[o]=Obj[o]++:Obj[o]=2):null
		return p
	});
	Object.keys(Obj).forEach(k=>{
		if(Obj[k]!==Math.max(...Object.values(Obj))){
			delete Obj[k]
		}
	});
	return Obj;
}
美煞众生 2022-05-04 13:55:09
'aaasdofjaopfjopaiiisjssfopiasdfffff'.match(/(.)1+/g)

得到的结果是

["aaa", "iii", "ss", "fffff"]

从这个数组里面找长度最长的元素并转化成需要的结果应该简单了吧

南汐寒笙箫 2022-05-04 13:55:09
const str114 = 'abcaakjbb';
	let r = str114.substr(1)
	const obj114 = str114.split('').reduce((pre, current) => {
		if (r === current) {
			pre[current] += 1
		} else {
			pre[current] = 1
		}
		r = current
		return pre; 
	}, {});
	const max114 = Math.max(...Object.values(obj114));
	for (const i in obj114) {
	  if (obj114[i] !== max114) delete obj114[i]
	}
	console.log(obj114);
黑色毁心梦 2022-05-04 13:55:09

习惯了用正则处理一些字符串的问题,感觉还可以优化,期待大佬版本

  • 添加最大长度为1的情况
function searchLongChar(str, max = 0, obj = {}) {
  // 提取重复串组合
  let arr = str.match(/(w)1+/g);
  if(arr==null) return;
  arr.map(v => {
    let l = v.length;
    if (max <= l) {
      // 重置数组
      if (max < l) obj = {};
      // 赋值
      obj[v.substr(-1)] = max = l;
    }
  })

  return obj;
}
于我来说 2022-05-04 13:55:09

Object.fromEntries 是 ES10 语法,使用高版本的浏览器或者 NodeJs 版本运行

function func(str) {
    let obj = str.split("").reduce((pre, cur) => {
        pre[cur] ? pre[cur]++ : pre[cur] = 1
        return pre
    }, {})
    let maxNum = Math.max(...Object.values(obj)) // 获取最多的次数
    let entries = Object.entries(obj).filter(([_, value]) => value === maxNum) // 过滤出出现最多次数的键值对
    return Object.fromEntries(entries) // 将键值对转换为对象(ES10 语法)
}

console.log(func('abcaakjbb'))
console.log(func('abbkejsbcccwqaa'))
心病无药医 2022-05-04 13:55:09
function operate(str){
	let obj = {},max = 1,temp=[]; // temp保存上一个值与连续次数 如[a,1];
	for(let i = 0;i<str.length;i++){
		if(!temp.length){
			temp = [str[i],1],max = 1,obj[str[i]] = 1;
		}else if(temp[0] == str[i]){
			temp[1] += 1;
			if(temp[1] > max){
				obj = {},max = temp[1],obj[temp[0]] = temp[1];
			}else if (temp[1] == max){
				obj[temp[0]] = temp[1]
			}
		}else{
			if(max == 1){
				temp = [str[i],1],obj[str[i]] = 1;
			}else{
				temp = [str[i],1]
			}
		}
	}
	return obj
	
}

console.log(operate('abcaakjbb'))  // {a: 2, b: 2}
console.log(operate('abcakjb')) // {a: 1, b: 1, c: 1, k: 1, j: 1}
console.log(operate('abbkejsbcccwqaa')) //{c: 3}
爱情眠于流年 2022-05-04 13:55:09
    function getMax (str) {
        var arr = [...new Set(str.split(''))]
        var _arr = []
        var _len = []
        arr.map((o, i) => {
            var reg = new RegExp(`(${o}+)`, 'g')
            var match = [...str.matchAll(reg)]
            var length = match.map((m, mi) => m[0].length).sort((a, b) => b - a)[0]
            _arr.push([o, length])
            _len.push(length)
        })
        var max = _len.sort((a, b) => b - a)[0]
        var maxArr = _arr.filter((o, i) => o[1] === max)
        return Object.fromEntries(maxArr)
    }

全面通用~~~
getMax('abc') // {a:1,b:1,c:1}
getMax('abcaakjbb') // {a: 2, b: 2}
getMax('abbkejsbcccwqaa') //{c: 3}
getMax('assssssssssssssssbaasdasdasdasgfsgsgsadgc') ////{s: 16}

深海少女心 2022-05-04 13:55:09

Object.fromEntries 是 ES10 语法,使用高版本的浏览器或者 NodeJs 版本运行

function func(str) {
    let obj = str.split("").reduce((pre, cur) => {
        pre[cur] ? pre[cur]++ : pre[cur] = 1
        return pre
    }, {})
    let maxNum = Math.max(...Object.values(obj)) // 获取最多的次数
    let entries = Object.entries(obj).filter(([_, value]) => value === maxNum) // 过滤出出现最多次数的键值对
    return Object.fromEntries(entries) // 将键值对转换为对象(ES10 语法)
}

console.log(func('abcaakjbb'))
console.log(func('abbkejsbcccwqaa'))

是连续出现最多,不是出现最多

清晨说ぺ晚安 2022-05-04 13:55:09

上面的麻烦学学markdown语法再发好吗?看着难受

在```后面加上javascript

function getMaxCharacters(str) {
  const map = {}
  var arr = str.match(/(w)1+/g)
  var max = arr[0].length
  arr.map(v => max = Math.max(max, v.length))
  const maxArr = arr.filter(v => v.length === max)
  maxArr.forEach(v => {
    map[v[0]] = v.length
  })
  return map
}

getMaxCharacters('abcaakjbb')  // { a: 2, b: 2 }
早茶月光 2022-05-04 13:55:09
const str = 'abbkejsbcccwqaa'

function fn(str) {
    let i = 0
    let result = {}
    let prev = ''
    let max = 0
    while(i < str.length) {
        const cur = str[i]
        if (prev === cur) {
            if (result[prev]) {
                result[prev]++
            } else {
                result[prev] = 2
            }

            max = Math.max(result[prev], max)
        }

        prev = str[i]
        i++
    }

    return Object.keys(result).reduce((prev, key) => {
        const val = result[key]
        
        if (val === max) {
            prev[key] = val
        }

        return prev
    }, {})

}

console.log(fn(str))
不美如何 2022-05-04 13:55:09

强行使用map保存了每个连续字符串的个数,然后在使用遍历找到出现次数最多的一个或者多个

function searchMaxStr(str) {
  let map = new Map();
  let result = {}//保存最后的结果
  let middle = str[0];
  let count = 0;
  for (let i = 0, length = str.length; i < length; i++) {
    let temp = str[i];
    if (temp == middle) {
      count++;
      if (map.has(temp)) {
        if (count > map.get(temp)) {
          map.set(temp, count)
        }
      } else {
        map.set(temp, count)
      }
    } else {
      count = 0;
      i = i - 1;
      middle = temp;
    }
  }
  let bianli = 0;
  for (let key of map.keys()) {
    value = map.get(key)
    if (bianli < value) {
      bianli = value;
      if (Object.keys(result) != 0) {
        result = {}
      } else {
        result[key] = value;
      }
    }
    if (bianli == value) {
      result[key] = value
    }
  }
  return result;
}
苦笑流年记忆 2022-05-04 13:55:09
function getCharts(str) {
    debugger;
    let obj = {};
    let max = {};
    let len = str.length;
    if (len > 1) {
        for (let i = 1; i < len; i++) {
            if (str[i - 1] == str[i]) {
                //obj[str[i]] += 1;
                if (!obj[str[i]]) {
                    obj[str[i]] = 2;
                } else {
                    obj[str[i]] += 1;
                }
            }
        }
    }
    let maxNum = Math.max.apply(null, Object.values(obj));
    for (let key in obj) {
        if (obj[key] == maxNum) {
            max[key] = maxNum;
        }
    }
    return max;
}
战皆罪 2022-05-04 13:55:09
function getMuitipleChar(str) {
        let result = {}
        var reg = /(w)1{1,}/g
        let multi = str.match(reg)
        if (multi && multi.length > 0) {
          result = multi.reduce((res, current) => {
            let currLen = current.length
            let keys = Object.keys(res)
            let len = res[keys[0]] || 0
            if (currLen < len) return res
            res[current[0]] = currLen
            if (currLen > len) {
              res = {
                [current[0]]: currLen
              }
            }
            return res
          }, {})
        }
        return result
      }
温柔一刀 2022-05-04 13:55:09
// 'abcaakjbb' => {'a':2,'b':2}
// 'abbkejsbcccwqaa' => {'c':3}
// 注意:题目说的是连续出现,注意连续二字
function getMaxTimes(str) {
  var maxMap = {};

  let currentTimes = 0;
  let maxTimes = 0;
  let char;
  let nextChar;

  for (let i = 0, len = str.length; i < len; ) {
    char = str[i];
    // 检查下一个...
    let j = 0;
    do {
      j++;
      nextChar = str[i + j];
      currentTimes++;
    } while (nextChar === char);

    maxMap[char] = currentTimes;
    // 顺便记录下最大出现次数
    if (currentTimes > maxTimes) maxTimes = currentTimes;
    currentTimes = 0;

    i += j;
  }
  // 去掉无用的记录
  Object.keys(maxMap).forEach(k => {
    if (maxMap[k] !== maxTimes) delete maxMap[k];
  });

  return maxMap;
}

console.log(getMaxTimes('abcaakjbb'));
console.log(getMaxTimes('abbkejsbcccwqaa'));
她如夕阳i 2022-05-04 13:55:09

空间复杂度: O(1);时间复杂度: O(N)

function getMaxContinuousStrLen(str) {
  if (typeof str !== 'string' || !str.length) {
    return ['', 0]
  }

  const length = str.length
  let last = str[0],
    times = 1,
    maxTimes = 0,
    maxChar = ''
  for (let i = 1; i < length; ++i) {
    if (str[i] === last) {
      ++times
    } else {
      if (times > maxTimes) {
        maxChar = last
        maxTimes = times
      }
      times = 1
      last = str[i]
    }
  }
  if (times > maxTimes) {
    maxChar = last
    maxTimes = times
  }

  return [maxChar, maxTimes]
}

// 测试代码
console.log(getMaxContinuousStrLen('b')) //[ 'b', 1 ] 
console.log(getMaxContinuousStrLen('abb')) //[ 'b', 2 ] 
console.log(getMaxContinuousStrLen('abbkejsbcccwqaa')) // [ 'c', 3 ]
console.log(getMaxContinuousStrLen('abcaakjbb')) // [ 'a', 2 ]
孤凫 2022-05-04 13:55:09
function findTargetStr(str) {
  return (str.match(/(.)1*/g) || []).reduce((res, cur) => 
    (
      cur.length > res.max ?
      (res.r = { [cur[0]]: cur.length }, res.max = cur.length) :
      (cur.length === res.max && (res.r[cur[0]] = res.max)
    ), res), { r: {}, max: 0 }).r
}
小红帽 2022-05-04 13:55:09
const findMaxConstantString = str => {
    const result = []
    str.replace(/(Sw)(1)+/g, matched => {
        result.push({[matched[0]]:matched.length})
      })
    return Object.assign(...result.filter( oRes => Object.values(oRes)[0] === Math.max(...result.map(counter =>Object.values(counter)[0]))))
}

//  好多大佬。。。献上我的渣渣版本 好多地方还可以优化
南薇 2022-05-04 13:55:09

function getMax(){
let str = 'bacdd33333';
let arr = [];
let arr1 = [];
let count = 1;
let max = null;
let obj = {};
for(let i=0;i<str.length;i++){
if(str[i] == str[i+1]){
count+=1;
}else{
count = 1;
}
arr1.push(count);
arr.push({key:str[i],count:count});
max = Math.max(...arr1);
}
for(let item of arr){
if(item.count == max){
obj[item.key] = item.count
}
}
return obj;
}
getMax()

菜鸟版本

风吹过旳痕迹 2022-05-04 13:55:09
const a = 'abcaakjbb'; const b = 'abbkejsbcccwqaa'

function findLetter (str = '') {
  const res = str.match(/(.)1+/g).reduce((acc, next) => {
    const maxLen = Math.max(Object.values(acc))
    const nextObj = { [next.charAt(0)]: next.length }
    const newAcc = maxLen > next.length ? acc : (maxLen === next.length ? Object.assign(acc, nextObj) : nextObj)
    return newAcc
  }, {})
  return res
}
console.log(findLetter(b)) // { c: 3 }
console.log(findLetter(a)) // { a: 2, b: 2 }
请别遗忘我 2022-05-04 13:55:09
function searchMaxStr(str) {
  const result = {}
  const continuous = str.match(/(w)1+/g)
  const max = continuous.reduce((max, curStr) => curStr.length > max ? curStr.length : max, 0)

  continuous.forEach(item => {
    if (item.length === max) {
      result[item] = max
    }
  })

  return result
}

searchMaxStr('abcaakjbb')
searchMaxStr('abbkejsbcccwqaa')
赠我空喜 2022-05-04 13:55:09
var s = 'awo[eigoawjeogpawkegak11111111111wef,.xcmvjawoegjawjegoaiwjsdm,.vmkadjsfawgefo eighaerg4a5g4'


function func(s){
    var Obj = s.split('').reduce((acct, cur) => {
        let item = acct[cur];
        item = item ? item + 1 : 1
        return { ...acct, [cur]: item }
    }, {})
    var max = Math.max(...Object.values(Obj))
    return Object.keys(Obj).reduce((acct, cur) => Obj[cur] === max ? { ...acct, [cur]: Obj[cur] }:acct, 
   {})
}
func(s)
晨敛清荷 2022-05-04 13:55:09

看大佬们都使用正则表达式进行匹配,小弟就不用正则来个版本

主要思路如下:

  1. 想要计算出现的次数,则需要遍历,这里通过Array.prototype.reduce方法,遍历每一个字符

  2. 题目又说是连续出现,那么只要单个字符出现2次以上,就要储存,所以这里在遍历的时候,通过reduce的第二个参数保存一个temp对象。

  3. 根据temp中该字符重复出现的次数就行逻辑判断。

    如果一旦发现temp对象中不存在[next]属性,则表示next并不是连续字符,所以完全覆盖temp对象

  4. 结果出来以后,删除strToObj中的temp属性。

  5. 通过Math.max以及Object.values方法找出最大值max

  6. filter对象strToObj,找出值等于max的。

  7. 最后在使用reduce拼接结果返回。

const findMaxRepeatString = str => {
  if (typeof str !== 'string') return {};
  const strToObj = Array.prototype.reduce.call(
    str,
    (pre, next) => {
      let track = pre.temp[next];
      if (!track) {
        pre.temp = {
          [next]: 1,
        };
        return pre;
      }
      track = ++pre.temp[next];
      if (track < 2) return pre;
      const v = track - (pre[next] || 0);
      pre[next] = v >= 1 ? track : pre[next];
      return pre;
    },
    { temp: {} }
  );
  delete strToObj.temp;
  const max = Math.max(...Object.values(strToObj));
  if (max < 2) return {};
  return Object.keys(strToObj)
    .filter(key => strToObj[key] === max)
    .reduce((pre, key) => {
      pre[key] = strToObj[key];
      return pre;
    }, {});
};

findMaxRepeatString ('aa11bbbppbbb'); // { b: 3 }
寄意 2022-05-04 13:55:09

一次遍历

function findLongest(str) {
  if (!str) return {}
  let count = 0
  let maxCount = 0
  let cur = str[0]
  let res = {}
  for (let i = 0; i < str.length; i++) {
    const s = str[i]
    if (s === cur) {
      count++
      if (count > maxCount) {
        res = { [s]: count }
        maxCount = count
      }
      if (count === maxCount) {
        res[s] = count
      }
    } else {
      count = 1
      cur = s
    }
  }
  return res
}

时间复杂度O(n), 空间复杂度O(1)

你这个 输入 ab 返回 {a:1}

掐死时间 2022-05-04 13:55:09
function count(str) {
    if (!str) return {};
    let hashMap = {};
    let i = 1;
    let cur = str[0];
    for (let j = 1; j < str.length; j++) {
        let temp = str[j];
        if (temp === cur) {
            i++;
            hashMap[temp] = i;
        } else {
            i = 1;
            cur = temp;
        }
    }
    let res = Object.keys(hashMap).map(key => hashMap[key]);
    res = Math.max(...res);
    for (let key in hashMap) {
        if (hashMap[key] < res) {
            delete hashMap[key]
        }
    }
    return hashMap
}
金兰素衣 2022-05-04 13:55:09
/*

第 114 题:编程题,找出字符串中连续出现最多的字符和个数(蘑菇街)
'abcaakjbb' => {'a':2,'b':2}
'abbkejsbcccwqaa' => {'c':3}

*/
function sequenceChar(str){
    const chars = str.split('');
    let count = 0;
    let max = 0
    let res = {};
    chars.reduce((pre,cur,index)=>{
        if(pre===cur){
            count++;
            if(count>max){
                res = {[pre]:count}
                max = count;
            }
            if(count === max){
                res[pre] = count;
            }
         
        }else{
            count=1;
            res[pre] = 1;
            if(index===chars.length-1){
                res[chars[index]] = count;
            }
        }
        return cur;
    });
    return res;
}

console.log(sequenceChar('abkejsbcwqa'));
南风几经秋- 2022-05-04 13:55:09
              function getMaxStr(str) {
                let map = {}
                let resultArr = []
                let max = Number.NEGATIVE_INFINITY
                let k = 1
                for (let i = 1; i < str.length; i++) {
                  if (str[i] == str[i-1]) {
                    k++
                    if (k > max) {
                      resultArr = [str[i]]
                    } 
                    if (k == max) {
                      resultArr.push(str[i])
                    }
                    // max = k
                    max = Math.max(k, max)

                  } else {
                    k = 1
                  }
                }
                console.log(resultArr, max)
                for (let i = 0; i < resultArr.length; i++) {
                  map[resultArr[i]] = max
                }
                return map
              }
山有枢 2022-05-04 13:55:09

function findNum(str){
let res = {};
let num = 1;
let max = num;
for(let i=0;i<str.length;i++){
if(str[i+1] === str[i]){
num++;
} else {
if(num>max){
res = {};
res[str[i]] = num;
max = num;
num = 1;
} else if(num === max){
res[str[i]] = num;
num = 1;
}
}
}
return res;
}
const data = 'abbkejsbcccwqaaaa';
console.log(findNum(data));

一次遍历完成,复杂度O(n)

〃安静 2022-05-04 13:55:09
// 编程题,找出字符串中 **连续** 出现最多的字符和个数(蘑菇街)

const str1 = 'abcaakjbb'
const result1 = { a: 2, b: 2 }

const str2 = 'abbkejsbcccwqaa'
const result2 = { c: 3 }

function findMax(str) {
  let map = {}
  let pre = ''
  let preLen = 0
  let cur = ''
  let curLen = 0
  for (let i = 0; i < str.length; i++) {
    const s = str[i]
    const next = str[i + 1]
    if (s === next) {
      cur = s
      curLen = curLen ? ++curLen : 2
    } else {
      if (curLen > preLen) {
        if (pre === cur) {
          preLen = curLen
          map[cur] = curLen
        } else {
          map = {
            [cur]: curLen
          }
          pre = cur
          preLen = curLen
        }
      }
      if (curLen === preLen) {
        if (pre !== cur) {
          map[cur] = curLen
        }
      }
      cur = ''
      curLen = 0
    }
  }

  console.log(map)
  return map
}

const find1 = findMax(str1)
console.log(JSON.stringify(find1) === JSON.stringify(result1))
const find2 = findMax(str2)
console.log(JSON.stringify(find2) === JSON.stringify(result2))
じ违心。 2022-05-04 13:55:09
'aaasdofjaopfjopaiiisjssfopiasdfffff'.match(/(.)1+/g)

得到的结果是

["aaa", "iii", "ss", "fffff"]

从这个数组里面找长度最长的元素并转化成需要的结果应该简单了吧

可以简化很多计算量!

阿楠 2022-05-04 13:55:09

Map集合连续出现次数
数据reduce获取最大次数 再生成结果

const getMaxNum = (str) => {
    let arr = str.split('');
    var map = new Map;
    var lastMsg = '';
    // arr.forEach(item => map.set(item, map.has(item) ? map.get(item) + 1 : 1))
    arr.forEach(item => {
        map.set(item, lastMsg === item ? map.get(item) + 1 : 1);
        lastMsg = item;
    });
    return [...map].reduce((pre, item) => {
        return item[1] > pre[0][1] ? [item] : item[1] === pre[0][1] ? pre.concat([item]) : pre;
    }, [['', 0]]).reduce((pre, item) => {
        return (pre[item[0]] = item[1]) && pre;
    }, {});
};
回首观望 2022-05-04 13:55:09
function find(str) {
  const reg = /(.)1+/g;
  const arr = str.match(reg).sort((a, b) => b.length - a.length);
  const maxLen = arr[0].length;
  let obj = { [arr[0][0]]: maxLen }
  for (let i = 1; i < arr.length; i++) {
    if (arr[i].length === maxLen) {
      obj = { ...obj, [arr[i]]: maxLen }
    } else {
      break;
    }
  }
  return obj
}
诠释孤独 2022-05-04 13:55:09
function maxChar (str) {
    var has = {}
    for (var i = 0; i < str.length; i++) {
        if (str[i] === str[i+1]) {
            has[str[i]] = has[str[i]] ? has[str[i]] + 1 : 2
        }
    }
    var max = 0, map = {}, keys = []
    for (var k in has) {
        if (has[k] >= max) {
            max = has[k]
            keys.push(k)
        }
    }
    for (var i = keys.length - 1; i >= 0; i--) {
        if (has[keys[i]] >= max) {
            map[keys[i]] = has[keys[i]]
        }
    }
    return map
}
勿忘初心 2022-05-04 13:55:09
function getMaxTimes(str) {
  if (!str) return {};

  let res = {};
  let m = 0; // 最大次数
  let i = 1; // 当前元素次数
  let j = 1; // 当前迭代位置
  while (j < str.length) {
    if (str[j] === str[j - 1]) {
      i++;
    } else {
      if (i > m) {
        res = { [str[j - 1]]: i };
        m = i;
      } else if (i === m) {
        res[str[j - 1]] = i;
      }
      i = 1;
    }
    j++;
  }

  // 处理最后的元素
  j = str.length - 1;
  if (i > m) {
    res = { [str[j]]: i };
  } else if (i === m) {
    res[str[j]] = i;
  }

  return res;
}
梦里南柯 2022-05-04 13:55:09
function maxSeq(str) {
      let _str = str+'*'
      let temp = {}
      let res = {}
      let max = 0
      for(let i=0;i<_str.length;i++){
        const letter = _str[i]
        if(temp[letter]){
          temp[letter]++
        }else{
          const key = Object.keys(temp)[0]
          const value = temp[key]
          if(i > 0){
            if(max<value){
              max = value
              res = {
                [key]: value
              }
            }else if(max===value){
              for(let i in res){
                if(res[i]<max){
                  delete res[i]
                }
              }
              res[key] = value
            }
            
          }else{
            res[letter] = 1
            max = 1
          }
          temp={
            [letter]: 1
          }
        }
      }
      return res
}
つ可否回来 2022-05-04 13:55:09
function getMax(str) {
    let l = 0,
        r = 0;
    const map = {};
    let maxCount = 0,
        count = 0;

    while (r <= str.length) {
        const char = str[r];

        if (char !== str[l]) {

            // 若字符和前面不相同,结束统计,更新最大值,窗口左端右移到当前位置,重置 count
            if (count >= maxCount) {
                maxChar = str[r-1];
                maxCount = count;
                if (map[maxCount]) {
                    map[maxCount].push(str[r-1]);
                } else {
                    map[maxCount] = [str[r-1]];
                }
            }
            count = 0;
            l = r;
            continue;
        }

        // 若字符和前面相同,继续统计,移动窗口右端点
        count ++;
        r ++;
    }
    const maxChars = map[maxCount];
    const result = {};

    maxChars.forEach(c => {
        result[c] = maxCount;
    });

    return result;
}

关于字符串和数组的“连续”问题,一般可以用滑动窗口来解决。
PS: 因为 maxCount 在遍历过程中会一直增加,如果用字符做 key,次数做 value,最后输出结果还需要找到最大的次数来输出,如果直接用当前 maxCount 做 key,出现过 maxCount 次的字符做值,最后只需在 map 中找到 key 为最后 maxCount 值的字符数组,里面的字符就是连续出现过最多次数的

初相遇 2022-05-04 13:55:09

双指针 一次遍历 除开输出 空间O(1)

`

function getMax(str){
   let res = { value : 0 }
   for(let i = 0; i < str.length; i++){
       let j = i
    while(str[i] == str[i + 1]) i++
    if(i - j + 1 > res.value){
        res.key = str[j]
        res.value = i - j + 1
    }
   }
   return res
}

`

最好是你 2022-05-04 13:55:09
function max (str) {
  let max = 0
  let char
  let prevchar
  const map = {}
  for (let c of str) {
    map[c] = map[c] ? (prevchar === c ? map[c] + 1 : 1) : 1
    if (map[c] > max) {
      max = map[c]
      char = c
    }
    prevchar = c
  }
  return { [char]: max }
}
夏末 2022-05-04 13:55:09

我写了个最low的,花了1.5小时。。。虽然周围很吵,但是一开始确实没思路

卧槽,我看错了,没注意是连续!!!以下代码找的是所有

const findTheMostFrequentStr = (str) => {
  let keys = [], values = []
  for (let i = 0; i < str.length; i++) {
    const index = keys.indexOf(str[i])
    if (index === -1) {
      keys.push(str[i])
      values.push(1)
    } else {
      values[index] += 1
    }
  }
 
  let obj = {}
  values.reduce((p, n, index) => {
    if (p === n) {
      obj[keys[index]] = n
      if (index === 1) {
        obj[keys[0]] = n
      }
      return n
    }
    if (p < n) {
      obj = {}
      obj[keys[index]] = n
      return n
    }
    if (p > n) {
      if (values[index - 1] === p) {
        obj[keys[index - 1]] = p
      }
      return p
    }
  })

  return obj
}

连续的low版实现(不会正则,第一次忘了Math.max)

const findTheMostFrequentStr = (str) => {
  let keys = [], values = []
  for (let i = 0; i < str.length; i++) {
    let index = keys.indexOf(str[i])
    if (index === -1) {
      keys.push(str[i])
      values.push(1)
    } else {
      // 判断是否连续
      const start = str.indexOf(str[i]) // 先找到目标在字符第一次出现的位置
      if (str.substring(start, i + 1).split('').every(item => item === str[i])) {
        values[index] += 1
      }
    }
  }

  const max = Math.max(...values)
  let obj = {}
  values.forEach((val, index) => {
    if (val === max) {
      obj[keys[index]] = max
    }
  })

  return obj
}
惟欲睡 2022-05-04 13:55:09
var entry = 'abcaakjbb'
function change(str){
    let left = right = 0,res = {},maxLen = 0
    while(right < str.length){
        if(str[left] === str[right + 1]){
            right++
        }else{
            let len = right - left + 1
            if(len > maxLen){
                maxLen = len
                res = {}
                res[str[left]] = len
            }else if(len == maxLen){
                res[str[left]] = len
            }
            left = ++right
        }
    }
    return res
}

console.log(change(entry))
一抹微笑 2022-05-04 13:55:09
// 找出 字符串中连续出现 最多的字符

function countChar(str) {
    var len = str.length
    if (len == 0) {
        return 0
    }
    if (len == 1) {
        return 0
    }
    var map = {}
    var max = 2
    var index = 0
    var next = 1
    while (next <= len) {
        if (str[index] != str[next]) {
            var leng = next - index
            if (!map[str[index]] || leng > map[str[index]]) {
                if (leng > max) {
                    map = {}
                    max = leng
                }
                if (leng >= max) {
                    map[str[index]] = leng
                }
            }
            index = next
            next++
        } else {
            next++
        }
    }
    return Object.keys(map).length ? map : 0

}
成熟稳重的好男人 2022-05-04 13:55:09
function getMaxCharacters(str) {
  const map = {}
  var arr = str.match(/(w)1+/g)
  var max = arr[0].length
  const maxArr = arr.filter(v => v.length === max)

  maxArr.forEach(v => {
    map[v] = v.length
  })
  return map
}

最终简化版

゛情释ヽ 2022-05-04 13:55:09
let str = 'abbkejsbcccwqaa'

function getMaxSame(str) {
	const len = str.length
	let result = {}
	const temp = {}
	for (let i = 0; i < len - 1; i++) {
		const char = str[i]
		if (char === str[i + 1]) {
			if (temp[char]) {
				temp[char] = temp[char] + 1
			} else {
				temp[char] = 2
			}
		} else {
		}
	}
	// 找到最大的
	let temp2
	for (let key in temp) {
		const value = temp[key]
		if (!temp2) {
			temp2 = value
			result[key] = value
			continue
		}
		if (value > temp2) {
			temp2 = value
			result = {}
			result[key] = temp2
		} else if (value === temp2) {
			result[key] = value
		}
	}
	console.log(temp)
	return result
}
console.log(getMaxSame(str))
久而酒知 2022-05-04 13:55:09
function getRepetCount(str) {
    let maxChars = '';
    let maxCount = 0;
    let length = str.length;
    let count = 1;
    for (let index = 1; index < length; index++) {
        const cur = str[index];
        const pre = str[index - 1];
        if (cur === pre) {
            count++;
        } else {
            if (count > maxCount) {
                maxCount = count;
                maxChars = pre;
            }
            count = 1;
        }
    }
    return {
        [maxChars]: maxCount
    }
}
撧情箌佬 2022-05-04 13:55:09
function getRepetCount(string) {
  const list = string.match(/(.)1*/g)

  let max = 0
  let object = {}
  for (const str of list) {
    if (str.length > max) {
      max = str.length
      object = {}
    }
    object[str] = str.length
  }

  return object
}
原来是傀儡 2022-05-04 13:55:09
let strss = 'abcaakjbbb'
const find11 = (str) => {
  let obj = {}, max = 0, res = {}
  for (let i = 0; i < str.length; i++) {
    if (obj[str[i]]) obj[str[i]]++
    else obj[str[i]] = 1
  }
  for (let key in obj) {
    let val = obj[key]
    if (val > max) {
      max = val
      res = {}
      res[key] = val
    }
    if (val === max) {
      res[key] = val
    }
  }
}
哀由 2022-05-04 13:55:09
function getMax(str){
        let max = 0
        const arr = str.split('') // 切割为数组
        const obj = {} // 记录长度
        arr.forEach(key =>{
            if(!(key in obj)){
                obj[key] = 0
            }
            obj[key]++
            max = Math.max(max,obj[key]) // 记录最大值
        })
        return  Object.entries(obj).filter(item => max == item[1]).reduce((prv,[key,val])=>{
            // 转化长度键值对 过滤长度与max相同的值 根据过滤的值创建对象并返回
            prv[key] = val
            return prv
        },{})
    }

    console.log(getMax('abcaakjbb'))



function getMax(str){
        let max = 0
        let arr = []
        let s = str
        const strs = [...new Set(str.split(''))] // 切割转数组 => 转set去冲 => 转数组 
        strs.forEach(key =>{ // 遍历去冲的数据
            let len = s.length // 获取字符串的长度
            s = s.replaceAll(key,'') // 替换字符串
            const diff = len - s.length // 计算替换了几个字符
            switch (true){ 
                case diff > max: // 如果替换的字符大于max
                    max = diff // 覆盖max
                    arr = [key] // arr 重置
                    break
                case diff == max: // 如果等于max
                    arr.push(key) // 推入arr中
                    break
            }
        })
        return arr.reduce((prv,key)=>{ // 聚合数据
            prv[key] = max
            return prv
        },{})
    }
ペ泪落弦音シ 2022-05-04 13:55:09
function find(str) {
  var res = {}
  // preChar:记录上一个字符,count:记录当前字符已连续次数,maxCount:表示最大连续字符次数
  var preChar = '', count = 0, maxCount = 0

  for(let i = 0; i< str.length; i++) {
    var cur = str[i]
    // 字符连续则累加,否则重置为 1
    if(cur === preChar) {
      count++
    } else {
      count = 1
    }
    preChar = cur

    if(count === maxCount) {
      // 多个字符连续次数相同的情况
      res[cur] = count
    } else if(count > maxCount){
      maxCount = count
      // 清空
      res = {}
      res[cur] = count
    }
  }
  return res
}

find('abcaakjbb') // {a: 2, b: 2}
find('abbkejsbcccwqaa') // {c: 3}
感性不性感 2022-05-04 13:55:09
// 双指针
function resolve (str) {
  let i = 0;
  let max = 0;
  const map = {};
  const result = {};
  while (i<str.length) {
    j = i + 1;
    while (j<=str.length) {
      if (str[i] !== str[j]) {
        if (!map[str[i]]) {
          map[str[i]] = 0;
        }
        map[str[i]] = Math.max(map[str[i]], j - i);
        max = Math.max(max, j - i);
        break;
      }
      j++;
    }
    i++;
  }
  for (let key in map) {
    if (map[key] === max) {
      result[key] = max;
    }
  }
  return result;
}

resolve('abcaakjbb')
怎言笑 2022-05-04 13:55:09
let str = 'abcaaakjbb';

        function findMax(str) {
            let obj = {};
            for (let i = 0; i < str.length; i++) {
                if (!obj[str[i]] && str[i] == str[i + 1]) {
                    obj[str[i]] = 2;
                } else if (obj[str[i]] && str[i] == str[i + 1]) {
                    obj[str[i]]++
                }
            }
            let res = {};
            let a = 0;
            for (const key in obj) {
                if (obj[key] > a) {
                    res = {};
                    res[key] = obj[key];
                    a = obj[key];
                } else if (obj[key] == a) {
                    res[key] = obj[key];
                }
            }
            console.log(res);
        }
        findMax(str)
长梦不多时 2022-05-04 13:55:03
function findChar(char) {
    let obj = {};
    let reg = /(w)1/g;

    char.match(reg).map(str = >Object.keys(obj).indexOf(str) > -1 ? obj[str] = obj[str] + str.length: obj[str] = str.length);

    return obj
}

findChar('abbkejsbbcccwqaa');
噩梦成真你也成魔 2022-05-04 13:54:57
const func = (str) => {
  let temp = '-1'
  let count = 0
  let maxCount = 0
  // 传入的字段串增加一位,防止以连续字母结尾的统计出错
  str += '-1'
  const res = {}
  
  while(str.length > 0) {
    // 相同字符串,统计+1
    if (temp === str[0]) {
      count ++
      res[temp] = count
    } else {
      // 遇到不同,比较当前连续的字符是否是最长的
      if (res[temp] >= maxCount) {
        maxCount = res[temp]
      } else {
        delete res[temp]
      }
      count = 1
      temp = str[0]
    }
    str = str.substring(1)
  }
  return res
}

// const str = 'abbkejsbcccwqaa'
const str = 'abcaakjbb'
const res = func(str)
console.log('res', res)
流心雨 2022-05-04 13:54:52
    function getMax(str) {
      let c = "";
      let res = Array.from(str).reduce((acc, cur) => {
       if (c == cur) {
          acc[cur] = acc[cur] ? acc[cur] + 1 : 2;
        } else {
          acc[cur] = 1;
        }
        c = cur;
        return acc;
      }, {});
      let max = Math.max.apply(null, Object.values(res));
      let ret = {};
      for (let k in res) {
        if (res[k] == max) {
          ret[k] = max;
        }
      }
      return ret;
    }
    console.log(getMax("abbkejsbcccwqaa")); //{c: 3}
    console.log(getMax("abcaakjbb")); //{a: 2, b: 2}
鲸落 2022-05-04 13:53:12

一次遍历

function findLongest(str) {
  if (!str) return {}
  let count = 0
  let maxCount = 0
  let cur = str[0]
  let res = {}
  for (let i = 0; i < str.length; i++) {
    const s = str[i]
    if (s === cur) {
      count++
      if (count > maxCount) {
        res = { [s]: count }
        maxCount = count
      }
      if (count === maxCount) {
        res[s] = count
      }
    } else {
      count = 1
      cur = s
    }
  }
  return res
}

时间复杂度O(n), 空间复杂度O(1)

亚希ぅ 2022-05-04 13:52:42

暴力版

function getStrMaxLengthObj (str) {
    if (!str) { return {} }
    let strObj = {}
    let res = {}
    let max = 0
    let currentLetter = ''

    for (let i = 0; i < str.length; i ++) {
        let item = str[i]

        if (currentLetter === item) {
            strObj[item] += 1
        } else {
            currentLetter = item
            strObj[item] = 1
        }

        if (strObj[item] > max) {
            max = strObj[item]
            res = {}
            res[item] = max
        } else if (strObj[item] === max) {
            res[item] = max
        }
    }

    return res
}

console.log(getStrMaxLengthObj('abbkejsbcccwqaa')) //- {c:3}
逆夏时光 2022-05-04 13:30:33
const arr = str.match(/(w)1*/g);
const maxLen = Math.max(...arr.map(s => s.length));
const result = arr.reduce((pre, curr) => {
  if (curr.length === maxLen) {
    pre[curr[0]] = curr.length;
  }
  return pre;
}, {});

console.log(result);
~没有更多了~

关于作者

0 文章
0 评论
22 人气
更多

推荐作者

已经忘了多久

文章 0 评论 0

15867725375

文章 0 评论 0

LonelySnow

文章 0 评论 0

走过海棠暮

文章 0 评论 0

轻许诺言

文章 0 评论 0

信馬由缰

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文