[Algorithm] Max Chars Problem

// --- Directions
// Given a string, return the character that is most
// commonly used in the string.
// --- Examples
// maxChar("abcccccccd") === "c"
// maxChar("apple 1231111") === "1"

function maxChar(str) {
  let m = {},
    max = -1,
    result = null;

  for (let char of str) {
    m[char] = m[char] + 1 || 1;
  }

  for (let [key, count] of Object.entries(m)) {
    max = Math.max(max, count);
    if (max === count) {
      result = key;
    }
  }

  return result;
}

module.exports = maxChar;

  

const maxChar = require(‘./index‘);

test(‘maxChar function exists‘, () => {
  expect(typeof maxChar).toEqual(‘function‘);
});

test(‘Finds the most frequently used char‘, () => {
  expect(maxChar(‘a‘)).toEqual(‘a‘);
  expect(maxChar(‘abcdefghijklmnaaaaa‘)).toEqual(‘a‘);
});

test(‘Works with numbers in the string‘, () => {
  expect(maxChar(‘ab1c1d1e1f1g1‘)).toEqual(‘1‘);
});

  

原文地址:https://www.cnblogs.com/Answer1215/p/10957803.html

时间: 2024-11-08 20:24:34

[Algorithm] Max Chars Problem的相关文章

hdu 2993 MAX Average Problem (斜率优化dp入门)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5855    Accepted Submission(s): 1456 Problem Description Consider a simple sequence which only contains positive integers as

MAX Average Problem(斜率优化dp)

MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7574    Accepted Submission(s): 1667 Problem Description Consider a simple sequence which only contains positive integers as

BNUOJ 3958 MAX Average Problem

MAX Average Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average v

hdu 2993 MAX Average Problem(斜率DP入门题)

题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训队论文<周源--浅谈数形结合思想在信息学竞赛中的应用> 这题输入有点大,要加读入优化才能过. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5

HDU 2993 MAX Average Problem(斜率优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Problem Description Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s

Berlekamp-Massey Algorithm [for Team Problem 5525]

Input: 第一行为两个正整数n,m 第二行为n个整数a1..an 最后一行为一个正整数k Output: 为一个整数,代表方案数对1000000007取模的值 Sample Input 5 3 1 1 2 0 2 2 Sample Output 3 来自毛爷爷17年论文 Berlekamp-Massey Algorithm直接开算 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const

HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的平均值最小. 解题思路:斜率DP经典题, 详细分析见: NOI2004年周源的论文<浅谈数形结合思想在信息学竞赛中的应用> 还有要注意要用输入输出外挂,不是getchar()版的,是fread()版的,第一次遇到这么变态的题目- -|||. 代码: 1 #include<iostream&g

【斜率优化】HDU 2993 MAX Average Problem

通道 题意:求出长度大于k子序列使得其各个元素之和的平均数最大,并输出最大平均值 思路:浅谈数形结合思想在信息学竞赛中的应用 代码: #include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int MAXN=100010; int sum[MAXN]; int q[M

[Algorithm] Array production problem

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. For example, if our input was [1, 2, 3, 4, 5], the expected output would be