Open sisterAn opened 4 years ago
const mostChars = function(str) {
if (!str) return '';
let stack = [];
let map = new Map();
str.split('').forEach(c => {
let last = stack.pop();
if(last === c) {
map.set(c, map.get(c) + 1);
stack.push(c);
} else {
stack.push(c);
map.set(c, 1);
}
})
let max = 0;
[...map.values()].forEach(n => {
if (max < n) max = n;
})
for (let key of map.keys()) {
if (map.get(key) < max) {
map.delete(key);
}
}
// console.log(Object.fromEntries(map));
return Object.fromEntries(map);
}
const maxRepeatLetter = str => {
const answer = {};
if (!str || !str.length) return answer;
if (str && str.length === 1) {
answer[str[0]] = 1;
return answer;
}
const map = {};
let max = 0,
prevLetter = '';
for (let i = 0; i < str.length; i++) {
if (prevLetter === str[i]) {
max++;
} else {
prevLetter = str[i];
max = 1;
}
if (!str[i + 1] || str[i] !== str[i + 1]) {
if (map[prevLetter]) {
map[prevLetter] = Math.max(map[prevLetter], max);
} else {
map[prevLetter] = max;
}
}
}
max = 0;
for (const key in map) {
if (map.hasOwnProperty(key)) {
max = Math.max(map[key], max);
}
}
for (const key in map) {
if (map.hasOwnProperty(key)) {
if (map[key] === max) {
answer[key] = max;
}
}
}
return answer;
};
function fn(str) {
if (!str) return {}
//vals存储当前满足的字符,max是数量,prev是上个字符,prevNum是上个字符的数量
let vals = [], max = 1
let prev = '', prevNum = 1
for (const le of str) {
if (le === prev) {
prevNum++
if (prevNum > max) {
vals = [prev]
max = prevNum
} else if(prevNum === max){
vals.push(prev)
}
} else {
prev = le
prevNum = 1
}
}
const res = vals.map(val => ({ [val]: max }))
return res
}
const maxRepeatLetter = str => {
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
}, {})
return result
}
const objOfRepeateString = (str) => {
const strList = [...str];
const len = strList.length;
const obj = Object.create(null);
let index = 0;
for (let i = 1; i < len; i++) {
const pre = strList[i - 1];
if (strList[i] !== pre) {
if(!obj[pre]) obj[pre] = i - index;
else obj[pre] = Math.max(i - index, obj[pre]);
index = i
}
}
const last = strList[len - 1];
obj[last] = obj[last] ? Math.max(len - index, obj[last]) : len - index;
return obj;
}
function mostChars(str) {
if (str === '') return '';
let stack = [];
let map = {}
let last = ''
str.split('').forEach(item=>{
if(stack.length) {
last = stack.pop()
}
if(last===item) {
map[item] = map[item]+1
} else {
map[item] = 1
}
stack.push(item)
})
console.log('map: ', map);
let max = 0;
Object.keys(map).forEach(item=>{
if(map[item]>max){
max = map[item]
}
})
Object.keys(map).forEach(item=>{
if(map[item]<max) {
delete map[item]
}
})
console.log('max>>>',max)
return map
}
let str = 'abcaakjbbb'
console.log(mostChars(str))
/**
* let str = 'abcaakjbb'=>{'a':2,'b':2}
*/
function mostChars(str) {
if (str === '') return '';
let stack = [];
let map = {}
let last = ''
str.split('').forEach(item=>{
if(stack.length) {
last = stack.pop()
}
if(last===item) {
map[item] = map[item]+1
} else {
map[item] = 1
}
stack.push(item)
})
console.log('map: ', map);
let max = 0;
Object.keys(map).forEach(item=>{
if(map[item]>max){
max = map[item]
}
})
Object.keys(map).forEach(item=>{
if(map[item]<max) {
delete map[item]
}
})
console.log('max>>>',max)
return map
}
let str = 'abcaakjbbb'
console.log(mostChars(str))
/**
* 2. 第二种方法
*/
const maxRepeatLetter = function(str) {
const arr = str.match(/(\w)\1*/g)//按连续分组
console.log('arr: ', arr);
let maxLen = 0
arr.forEach(item=>{
if(item.length>maxLen) {
maxLen = item.length
}
})
console.log('maxLen: ', maxLen);
const res = arr.reduce((pre,cur)=>{
if(cur.length===maxLen) {
pre[cur[0]] = cur.length
}
return pre
},{});
return res;
}
console.log(maxRepeatLetter(str))
function getMax(str) {
//若要保证字符添加的顺序,可考虑使用new Map()
let map = {};
let res = {};
let max = 0;
let curChar = '';
for (let i = 0; i < str.length; i++) {
const item = str[i];
if (item === curChar) {
// 相同字符串加1
map[item] += 1;
} else {
//出现不连续字符的时候,初始化当前字符出现次数为1,
map[item] = 1;
curChar = item;
}
// 若当前字符的最大次数大于已记录的最大次数(至于是哪个字符,我们不用关心)
if (map[item] > max) {
max = map[item];
// 清空结果对象,只记录当前字符的最大次数
res = {
[item]: max
};
} else if (map[item] === max) {
// 若最大次数已经出现过,则追加记录当前字符的次数,可能就是同一字符,也可能不是
res[item] = max;
}
}
return res;
}
console.log(getMax('abcaakjbb'))
function getMostChar(str){ let i = 0 let obj = [] while(i<str.length){ let val = str.charAt(i) let count = 0 while(i<str.length&&str.charAt(i)===val){ count++ i++ } let key = count+'-'+val obj[key]=count } let maxValue = 0 let result = {} for(let key in obj){ if(obj[key]>maxValue){ result = {} maxValue = obj[key] let val = key.split('-')[0] let k = key.split('-')[1] result[k] = val }else if(obj[key]===maxValue){ let val = key.split('-')[0] let k = key.split('-')[1] if(!result.hasOwnProperty(k)){ result[k] = val } } } return result } console.log(getMostChar('abbkejsbcccwqaaaqqqwwwccc'))
注意:题目说的是连续出现,注意连续二字