Header Ad

HackerEarth Marut and Strings problem solution

In this HackerEarth Marut and Strings problem solution Marut loves good strings. According to him, good strings are those which contain either all alphabets of uppercase or lowercase. While he is preparing for his exams, he finds many bad strings in his book and wants to convert them to good strings. But he wants to do this in minimum number of operations.
In one operation, he can pick only one character of any case and convert it to any other case.

As his exams are going on, he wants your help.


HackerEarth Marut and Strings problem solution


HackerEarth Marut and Strings problem solution.

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <set>
#include <assert.h>
#include <cstring>
#include <string>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <time.h>
#include <climits>

using namespace std;

#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORR(i,a,b) for(int i=a;i>=b;--i)
#define FORC(it,container) for(typeof(container.begin()) it=container.begin();it!=container.end();++it)
#define INT(x) scanf("%d",&x)
#define LLD(x) scanf("%lld",&x)
#define STR(x) scanf("%s",x)
#define CHAR(x) scanf("%c",&x)
#define PINT(x) printf("%d\n",x)
#define PLLD(x) printf("%lld\n",x)
#define CLR(x) memset(x,0,sizeof(x));
#define F first
#define S second
#define PB push_back

const int INF = INT_MAX;
const int MAX = 100005;
const int MOD = 1e9 + 7;

int main()
{
freopen("in","r",stdin);
freopen("out","w",stdout);
int test;
cin>>test;
if (test<1 || test>10)
{
cout<<"Invalid Test";
return 0;
}
while(test--)
{

string s;
cin>>s;

int len = s.length();

if(len<1 || len>100000)
{
cout<<"Invalid Input\n";
continue;
}
int ans=0;
int ans1=0,ans2=0;

FOR(i,0,len) {

assert((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'));

if(s[i]>='a' && s[i]<='z')
++ans1;
else if(s[i]>='A' && s[i]<='Z')
++ans2;

}

ans = min(ans1,ans2);
cout<<ans<<endl;

}
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;

int main()
{
int T;
cin>>T;
assert(T>=1 && T<=10);
while(T--)
{
int l=0,u=0;
char str[100001];
cin>>str;
int len=strlen(str);

assert(len>=1 && len<=100000);
for(int i=0;str[i]!='\0';i++)
{
assert((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'));
if(islower(str[i]))
l++;
else
u++;
}

cout<<min(l,u)<<endl;
}
return 0;
}



Post a Comment

0 Comments