Google questions

Collect Coins With Tokens

GoogleMedium

A board is a string of cells. A . is an empty cell, a T holds a token, and a C holds a coin.

Implement max_coins.

Part 1: Collect coins by moving tokens

Given the string board, return the largest number of coins you can collect.

In one move, you pick a token and move it exactly 3 cells to the right. The token must stay on the board and may not land on a cell holding another token, but it may pass over any cells in between, tokens and coins included. A token that lands on a coin collects it, and that cell becomes empty; passing over a coin collects nothing.

You can make any number of moves, in any order, with any token. A token with no legal move stays where it is.

  1. Example 1
    board
    "T..C..C.."
    Output
    2

    Why: The token moves from index 0 to 3 and collects that coin, then from 3 to 6 for the second one.

  2. Example 2
    board
    "T.C...T.C"
    Output
    0

    Why: The token at index 0 can only land on indices 0, 3 and 6, and the token at index 6 cannot move (index 9 is off the board). The coins at indices 2 and 8 sit on cells no token ever lands on.

Constraints

  • 1 <= len(board) <= 10^5
  • board[i] is ., T or C
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Collect Coins With Tokens: Google interview question | LoopCoach