Practice

Contest

Author: Praveen Dhinwa

Tester: Misha Chorniy

Editorialist: Animesh Fatehpuria

### PROBLEM

You just received a message given by a string s. You don’t know whether this message is sent by Chef or his brother. Also, the communication channel through which you received the message is erroneous and hence can flip a letter from uppercase to lowercase or vice versa. However, you know that this channel can make at most k such flips.

Chef always sends messages to his friends in all small letters, whereas the little brother sends messages in all capital letters. Determine whether the message could have been sent only by Chef, only by the little brother, by both or by none.

### EXPLANATION

Number of uppercase letters <= k implies that it could have been Chef. This is because we can consider all the uppercase letters as “flips” made by the communication channel, which would imply that our original message comprised only of lowercase letters. Similarly, number of lowercase letters <= k implies that it could have been Chef’s brother. Here is the implementation in Python:

``````t = int(input())
for _ in range(t):
(n, k) = list(map(int, input().split(' ')))
s = input()
chef = sum([1 for ch in s if ch >= 'A' and ch <= 'Z']) <= k
brother = sum([1 for ch in s if ch >= 'a' and ch <= 'z']) <= k
if (chef and brother):
print("both")
elif (chef):
print("chef")
elif (brother):
print("brother")
else:
print("none")
``````

### AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.

I need help with my code.

Chef is a really nice and respectful person, in sharp contrast to his little brother, who is a very nasty and disrespectful person. Chef always sends messages to his friends in all small letters, whereas the little brother sends messages in all capital letters.

You just received a message given by a string s. You don’t know whether this message is sent by Chef or his brother. Also, the communication channel through which you received the message is erroneous and hence can flip a letter from uppercase to lowercase or vice versa. However, you know that this channel can make at most K such flips.

Determine whether the message could have been sent only by Chef, only by the little brother, by both or by none.

In the main fucntion, look at these lines:

``````char arr[n];
cin>>arr;
``````

Issues:

• You shouldn’t declare static allocation inside the main function. These allocations will be done in stack space and with each test case the allocation will be done again. An alternate nice way of doing the same is to either do a dynamic memory allocation or declare “char arr[105]” globally. Note that the size of array is taken 105 which is greater than largest value of N.

• Use scanf for reading a string. If you want to use cin, you can do with string class in C++.

int main() {
string s;
cin >> s;
}

``````#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);

ll t;
cin >> t;
while(t--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
int upper = 0;
int lower = 0;
for(int i = 0; i < n; i++) {
if(int(s[i]) >= 65 && int(s[i]) <= 90) {
upper++;
}  else {
lower++;
}
}
if(lower <= k && upper > k) {
cout << "brother" << endl;
} else if(lower > k && upper <= k) {
cout << "chef" << endl;
} else if(lower <= k && upper <= k) {
cout << "both" << endl;
} else {
cout << "none" << endl;
}
}
}
``````

I did a very simple way but instead of doing if the character of the string is uppercase I used the int of the character. I think that my way is worse. But I still got it right.

``````#include <bits/stdc++.h>

#define ll long long int
#define ln "\n"
#define fast ios::sync_with_stdio(0); cin.tie(nullptr);

using namespace std;

string solve(string s, int k){
int up=0;
int lo=0;
for(char c: s){
if(isupper(c)){
up++;
}
else if(islower(c)){
lo++;
}
}
// cout << up << " " << lo;
if(lo <= k && up > k){
return "brother";
}
else if(up <= k && lo <= k){
return "both";
}
else if(lo > k && up <= k){
return "chef";
}
return "none";
}

int main(){
fast;

int t;
cin >> t;

while(t--){
int n, k;
string s;
cin >> n >> k;
cin >> s;
cout << solve(s, k) << ln;
}
return 0;
}``````