PROBLEM LINK:
Author: Piyush Kumar
Tester: Minako Kojima
Editorialist: Pawel Kacprzak
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Adhoc
PROBLEM:
Indian people use only gestures identified by ‘I’ and ‘N’. Non Indian people use only gestures identified by ‘Y’ and ‘N’. Given a string consisting of only ‘Y’, ‘N’ and ‘I’, decide if the string represents an Indian person, non Indian person or it is impossible to determine which one it represents. For each of these decision answers are: “INDIAN”, “NOT INDIAN” and “NOT SURE”.
QUICK EXPLANATION:
You need to decide if the string contains at least one ‘Y’ or at least one ‘I’ or neither of these letters is presented in the string.
EXPLANATION:
Problem statement ensures that it is impossible to have both ‘Y’ and ‘I’ in the string.
Based on that fact we can easily compute the answer.
If the string contains at least one ‘I’, then any other letter in is it ‘I’ or ‘N’, so the person represented by this string is Indian, hence the answer is “INDIAN”.
On the other hand, if the string contains at least one ‘Y’, then any other letter in it is ‘Y’ or ‘N’, so the person represented by this string is non Indian, hence the answer is “NOT INDIAN”.
Otherwise, if there is no ‘I’ and ‘Y’ in the string, each letter in it is ‘N’, so we cannot be sure if it represents Indian or non Indian, hence the answer is “NOT SURE”.
In order to do that, you can iterate over the string in a single loop:
Pseudo Code
found = 0 for(i = 0; i < s.length(); ++i) if (s[i] != 'N') if(s[i] == 'Y') res = "NOT INDIAN" else if (s[i] == 'I') res = "INDIAN" found = 1 break if(found==0) res = "NOT SURE" print res
Complexity
O(n) since we just need a single pass over the string
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.