Problem · Simulation

Count Tiles in 1D Othello

Learn this problem
MediumMFMoney ForwardOA

Problem statement

1D Othello is a board game played with Othello tiles and a horizontal grid. The game is played as follows:

  • The game pieces are tiles in which one side is black and the other side is white.
  • There are two players. One player plays black tiles (the black side of the tiles), the other plays white tiles (the white side of the tiles).
  • The game begins with two tiles next to each other.The left tile is black and the right tile is white.
  • The players take turns placing a tile on the board. A player cannot pass their turn.
  • Tiles can only be placed next to a tile that is already on the board. Therefore, there are only two positions in which a tile can be placed on each turn (to the left or right of the tiles already on the board).
  • After placing a tile, the player flips all of the tiles between the new tile and the nearest same-color tile. If the tile next to the new tile is the same color or there are no same-color tiles, the player does not flip any tiles.
  • The player must place a tile even when he cannot flip any tiles.
  • A transcript of 1D Othello is written in a string consisting of L and R. If the i-th letter of the transcript is L, that means a tile (a black tile when i is an odd number and a white tile when i is an even number) was placed to the left of the tiles already on the board, and if it is R that means a tile was placed to the right of the tiles already on the board.

    You are given a transcript S of the game. Please display the respective number of black and white tiles at conclusion of the game.

    Function

    countTiles(s: String) → int[]

    Examples

    Example 1

    s = "RRLL"return = [0, 6]
    The game proceeded as follows: bw(initial)->bbb->bbbw->bbbbw->wwwwww Hence, all six white tiles at the end of the game.

    Example 2

    s = "LLRLRLR"return = [3, 6]
    🐣

    More Money Forward problems

    drafts saved locally
    public int[] countTiles(String s) {
      // write your code here
    }
    
    s"RRLL"
    expected[0, 6]
    checking account