Open habc0807 opened 4 years ago
'wodexiaomaojiaoxiaohei'.split('').sort().join('').match(/(.)\1+/g).sort((a,b)=>b.length-a.length)[0]
const str = "wodexiaomaojiaoxiaohei";
function fn(str) {
if (!str.length) return;
str = str.split("").sort();
let left = 0;
let char = str[left];
let max_len = 0;
for (let right = 1; right < str.length; right++) {
if (str[right - 1] !== str[right]) {
if (right - left > max_len) {
max_len = right - left;
char = str[right - 1];
}
left = right;
}
}
return [char, max_len];
}
console.log(fn(str));
var str = ‘wodexiaomaojiaoxiaohei’;