PROBLEM LINK:
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.