Marut and Strings

\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 a 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.

Example:

Input: s = "abcEfg"
Output: 1

Approach

C++

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

void marutAndString(string s)
{
    if (s.size() > 100)
        cout << "Invalid Input\n";
    else
    {
        int cnt1 = 0, cnt2 = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] >= 'A' && s[i] <= 'Z')
                cnt1++;
            else if (s[i] >= 'a' && s[i] <= 'z')
                cnt2++;
        }
        if (cnt1 == 0 && cnt2 == 0)
            cout << "Invalid Input\n";
        else
            cout << min(cnt1, cnt2) << "\n";
    }
}
int main()
{

    string s = "abcEfg";

    marutAndString(s);

    return 0;
}


No comments:

Post a Comment