There was a question in topcoder.


Three kids are playing the following game: A word is written on a sheet of paper, and one after another, the players take turns adding letters to the word. During each turn, a player must add a single letter to either the beginning or the end of the word. If a player cannot add a letter during his turn, the game ends and that player is declared the loser.

During the game, a player might cheat by making an illegal move. Any move where the player does not add exactly one letter to either the beginning or the end of the word is considered illegal. The only exception is the last move of the game, when the loser adds no letters to the word. Given the log of the game, you must determine whether any of the players cheated.

You are also given the String[]s first, second, and third, containing the chronological lists of words seen by each of the players at the beginning of their turns. Element 0 of first is therefore the original word, element 0 of second is the word after the first player makes his first move, element 0 of third is the word after the second player makes his first move, etc. Return 1, 2, or 3 if the first, second, or third player, respectively, cheated. If multiple players cheated, return the player who cheated first. If nobody cheated, return -1.






Returns: 2


The second player saw "hello" before making his second move, and then, the third player saw "ello" immediately after the second player made his second move. Therefore, the second player cheated on his second move because "hello" -> "ello" is not a legal move.

I didn't understand the question itself. if someone understood how to do this question, Please help me .

1 Year
Discussion Span
Last Post by Nutster

As I understand the specification, each turn a player is supposed to add a letter either to the beginning or to the end of the previous word. You are not checking to see if this is a valid English word, but if the players made valid moves. Did a player add a letter to the previous word? If not, then report that player by returning their player number. If nobody goofed, or cheated as stated in the specification, then return -1 from your function.

The input is given as 3 lines representing each player got to play with, with the first line indicating what the first player got, the second line showing what the second player received and the third line showing what the third player got. Each player would then alter the word and give it to the next player.

In the example shown, the starting word is "e", to which Player 1 adds a letter. Player 2 sees "el" so player 1 had added an "l" to the end. Player 3 gets "ell", so Player 2 added another "l". It is Player 1's turn again with the word "ello" coming from player 3. Player 1 prepends an "h" and it goes to Player 2. Player 2 removes a letter (against the rules) and gives the word to Player 3, who sees "ello". Because Player 2 did not add a letter on one of the plays, return 2.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.