FastPrepMaximum Coins With Moving Tokens
Problem · Dynamic Programming

Maximum Coins With Moving Tokens

Learn this problem
MediumGoogle logoGoogleINTERNOA
See Google hiring insights

Problem statement

There is a single-player board game with N positions described by a string board. Each position is empty ('.'), contains a player's token ('T'), or contains a coin ('C'). The player may have multiple tokens.

A coin is collected when a token is placed on the coin's position. Each coin can be collected only once.

In one turn, the player may move one token exactly three positions to the right. The token does not stop on the positions in between, and every token may be moved multiple times. A token cannot be moved if another token already occupies its destination.

Return the maximum number of coins the player can collect.

Implement solution(board), where board is a string of length N.

Function

solution(board: String) → int

Examples

Example 1

board = "TT.T.CCCCC"return = 3

The player can move the third and second tokens twice, collecting three coins in total:

"TT.T.CCCCC" -> "TT...CTCCC" -> "TT...C.CCT" -> "T...TC.CCT" -> "T....C.TCT".

It is still possible to move the first token, but it cannot collect any coins.

Example 2

board = "T...CCCC"return = 1

Example 3

board = "C..TT.CT.C"return = 2

Constraints

  • N is an integer within the range [1..100].
  • String board consists only of the characters '.', 'T' and/or 'C'.

More Google problems

drafts saved locally
public int solution(String board) {
  // write your code here
}
board"TT.T.CCCCC"
expected3
checking account