CHMNY - Editorial

PROBLEM LINK:

Practice
Contest

Author: Vikas Yadav
Tester: Lalit Singh
Editorialist: Vikas Yadav

DIFFICULTY:

EASY

PREREQUISITES:

String

PROBLEM:

In the given string S, which contains G for guard, M for money, T for the thief and for blank space; check if there is guard between money and thief or not.

QUICK EXPLANATION:

Remove all blank spaces * from the string after that check if "MT" or "TM" is present in the string print “NO” otherwise print “YES”.

EXPLANATION:

From the given string create a new string and copy all the characters except * in same order. Now check whether thief T is present just before or after the money M then money is not safe and print “NO” otherwise print “YES”.

Common Mistake:

Print the output “YES” or “NO” (uppercase)
G, T, M, ∗ can present 0 (zero) or more times in the string.

SOLUTIONS:

Setter's Solution
// Chef and Money
// G  for a guard, M for money, T for the thief and ∗ for blank space.

#include <stdio.h>

int main()
{
	int test_cases;
	scanf("%d", &test_cases);

	while(test_cases--)
	{
		char str[26], new_str[26];
		scanf("%s", str);

		int j = 0, flag = 0;

		for(int i = 0; str[i] != '\0'; i++)
		{
			if(str[i] != '*')
				new_str[j++] = str[i];
		}

		new_str[j] = '\0';

		for(int i = 1; new_str[i] != '\0'; i++)
		{
			if((new_str[i-1] == 'M' && new_str[i] == 'T') || (new_str[i] == 'M' && new_str[i-1] == 'T'))
			{
				flag = 1;
				break;
			}
		}

		printf(flag ? "NO\n" : "YES\n");

	}

	return 0;
}

Solution in Python
# Chef and Money

test_cases = int(input())
while test_cases:
  str = input().replace('*', '')
  if 'MT' in str or 'TM' in str:
  	print('NO')
  else:
  	print('YES')
  test_cases -= 1