Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第11题(2019-08-05): 编程题,找出字符串中连续出现最多的字符和个数 #5

Open
qappleh opened this issue Aug 5, 2019 · 2 comments

Comments

@qappleh
Copy link
Owner

qappleh commented Aug 5, 2019

'abcaakjbb' => {'a':2,'b':2}
'abbkejsbcccwqaa' => {'c':3}  
@qappleh
Copy link
Owner Author

qappleh commented Aug 6, 2019

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);

@qappleh qappleh changed the title 第11题(2019-08-05): 编程题,找出字符串中连续出现最多的字符和个数(蘑菇街) 第11题(2019-08-05): 编程题,找出字符串中连续出现最多的字符和个数 Aug 20, 2019
@qiqingfu
Copy link

qiqingfu commented Aug 1, 2020

不考虑空间和时间复杂度,- .. -

function findMost(str) {
  const m = new Map();
  for (let s of str) {
    if (m.has(s)) {
      m.set(s, m.get(s) + 1);
    } else {
      m.set(s, 1);
    }
  }

  const maxValue = Math.max.apply(null, [...m.values()]);
  let res = {};
  for (let [k, v] of m.entries()) {
    if (maxValue === v) {
      res[k] = v;
    }
  }

  return res;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants