NEWGAME - Editorial

PROBLEM LINK:

https://www.codechef.com/RSC2017/problems/NEWGAME

Author: spraveenkumar

DIFFICULTY:

CAKEWALK

PROBLEM:

There are cards which has a number from 0 - 9. Each play one card at a time. Initially the game is towards Chef side. But if they play 2 same cards(have same number on them), then the game shifts towards the opponent’s side. If again they play 2 same cards, the game again shits towards Chef side. When all the cards are played, if the game was on Chef’s side, he wins. Else the opponent wins.

You are given a string of the cards both played in order. Compute if Chef wins the game or not. Output “WIN” without quotes if Chef wins else LOSE without quotes,

QUICK EXPLANATION:

This is basic String manipulation. Maintain a boolean variable denoting the winner. Iterate through each character in the String, and if they are equal modify the boolean variable.

EXPLANATION:

The simple and efficient solution is to iterate over the characters of both strings simultaneously to see if they match. The winner can be represented with a boolean variable. When the cards match, this boolean value is altered by setting it to it’s complement. ie: var = !var. Then at the end the boolean value tells the winner.

AUTHOR’s SOLUTION:

import java.util.Scanner;
class Game{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while(t-- !=0){
	    String m1 = sc.next();
	    String m2 = sc.next();
	    boolean win = true;
	    for(int i = 0; i<m1.length(); i++){
		if(m1.charAt(i)==m2.charAt(i))
			win=!win;
	    }
	    if(win)
	    	System.out.println("WIN");
		else
		System.out.println("LOSE");

	}
	sc.close();
    }
}