Maximum Occuring Character
PROBLEM :
Given a string, find the maximum occurring character in the string. If more than one character occurs maximum number of time then print the lexicographically smaller character.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a separate line.
Output:
Print the lexicographically smaller character which occurred the maximum time.
Constraints:
1 = T = 30
1 = |s| = 100
Example:
Input:
2
testsample
geeksforgeeks
Output:
e
e
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include <iostream>
using namespace std;
#include<string.h>
void maximum(char []) ;
int main() {
int t ;
char ch[200] ;
cin>>t ;
while(t--)
{
cin>>ch ;
maximum(ch) ;
cout<<endl ;
}
return 0;
}
void maximum(char ch[])
{
int l,i,max ;
char c='z' ;
l=strlen(ch) ;
int count[256]={0} ;
for(i=0;i<l;i++)
count[ch[i]]++ ;
max=-1 ;
for(i=0;i<l;i++)
{
if(max<count[ch[i]])
{
max=count[ch[i]] ;
c=ch[i] ;
}
if(max==count[ch[i]]&&ch[i]<c)
{
max=count[ch[i]] ;
c=ch[i] ;
}
}
cout<<c ;
}
---------------------------------------------------------------------------------
Given a string, find the maximum occurring character in the string. If more than one character occurs maximum number of time then print the lexicographically smaller character.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case consist of a string in 'lowercase' only in a separate line.
Output:
Print the lexicographically smaller character which occurred the maximum time.
Constraints:
1 = T = 30
1 = |s| = 100
Example:
Input:
2
testsample
geeksforgeeks
Output:
e
e
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include <iostream>
using namespace std;
#include<string.h>
void maximum(char []) ;
int main() {
int t ;
char ch[200] ;
cin>>t ;
while(t--)
{
cin>>ch ;
maximum(ch) ;
cout<<endl ;
}
return 0;
}
void maximum(char ch[])
{
int l,i,max ;
char c='z' ;
l=strlen(ch) ;
int count[256]={0} ;
for(i=0;i<l;i++)
count[ch[i]]++ ;
max=-1 ;
for(i=0;i<l;i++)
{
if(max<count[ch[i]])
{
max=count[ch[i]] ;
c=ch[i] ;
}
if(max==count[ch[i]]&&ch[i]<c)
{
max=count[ch[i]] ;
c=ch[i] ;
}
}
cout<<c ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment