Header Ad

HackerEarth Maximum occurrence problem solution

In this HackerEarth Maximum occurrence problem solution You are given a string which comprises of lower case alphabets (a-z), upper case alphabets (A-Z), numbers, (0-9), and special characters like !,-.; etc.

You are supposed to find out which character occurs the maximum number of times and the number of its occurrence, in the given string. If two characters occur an equal number of times, you have to output the character with the lower ASCII value.

For example, if your string was: aaaaAAAA, your output would be A 4, because A has a lower ASCII value than a.

HackerEarth Maximum occurrence problem solution

HackerEarth Maximum occurrence problem solution.

#include <bits/stdc++.h>

using namespace std;

int freq[3000];
char s[1005];

int main() {
int maxFreq = 0, n;
char ch;
cin.getline(s, 1000);
n = strlen(s);
for (int i = 0; i < n; ++i) {
if (freq[s[i]] > maxFreq) {
maxFreq = freq[s[i]];
ch = s[i];
} else if (freq[s[i]] == maxFreq && ch > s[i]) {
ch = s[i];
cout << ch << " " << maxFreq;
return 0;

Second solution

s = raw_input()
assert(len(s)>0 and len(s)<1001)
ans = [0]*300
for i in s:
final, mx = "", 0
for i in xrange(len(ans)):
if ans[i] > mx:
mx = ans[i]
final = chr(i)
print final, mx

Post a Comment