Count Tiles in 1D Othello
Updated
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 (counting from 1) 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. Return an array of two integers: the number of black tiles and the number of white tiles on the board at the end of the game, in that order.
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]The game proceeds as follows (b = black, w = white):
- Start: bw.
- Move 1 (black, L): bbw. The neighbor is black, so nothing flips.
- Move 2 (white, L): wbbw. The two black tiles between the new tile and the nearest white tile flip: wwww.
- Move 3 (black, R): wwwwb. There is no other black tile, so nothing flips.
- Move 4 (white, L): wwwwwb.
- Move 5 (black, R): wwwwwbb.
- Move 6 (white, L): wwwwwwbb.
- Move 7 (black, R): wwwwwwbbb.
The board ends with 3 black tiles and 6 white tiles, so the answer is [3, 6].
Constraints
1 ≤ s.length ≤ 105- Each character of
sis'L'or'R'.
More Money Forward interview questions
Similar questions at other companies
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Stabilize Student Test ScoresVirtu Financial · OAVirtu Financial interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
- Index of the Maximum After Left RotationsChubb · OAChubb interview questions
- Leaves Remaining After WindCresta · OACresta interview questions
- Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions