Last and First
Lv2
wordsCount: 578
readingTime: 3 mins
viewers:
Problem
Several people are playing the game of English word chain where each player takes turns saying a word that must begin with the last letter of the previous word. Here are the rules:
- Players take turns in a sequence from 1 to
n
. - After the last player, it starts again with the first player.
- The word must start with the last letter of the word the previous player said.
- A word cannot be repeated.
- A word must be more than one letter.
For example, if 3 people are playing:
- tank → kick → know → wheel → land → dream → mother → robot → tank
The game progresses as follows: - Player 1: tank
- Player 2: kick
- Player 3: know
- Player 1: wheel (continues…)
The game ends when a player repeats a word previously said or says a word that does not follow the rule of starting with the last letter of the previous word.
Given the number of players n
and the list of words words
spoken in order, the task is to determine the first player to be disqualified and on which turn they are disqualified. If no player is disqualified based on the provided words, return [0, 0].
Constraints
- The number of players
n
is a natural number between 2 and 10. words
is an array of words used in the game, its length is betweenn
and 100.- Each word’s length is between 2 and 50 characters.
- All words consist of lowercase English letters.
- The meaning of the words is not considered in this game.
Example
n | words | result |
---|---|---|
3 | [“tank”, “kick”, “know”, “wheel”, “land”, “dream”, “mother”, “robot”, “tank”] | [3, 3] |
5 | [“hello”, “observe”, “effect”, “take”, “either”, “recognize”, “encourage”, “ensure”, “establish”, “hang”, “gather”, “refer”, “reference”, “estimate”, “executive”] | [0, 0] |
2 | [“hello”, “one”, “even”, “never”, “now”, “world”, “draw”] | [1, 3] |
Explanation of Examples
- Example 1:
- With 3 players, the sequence of words goes as: Player 1: tank, wheel, mother; Player 2: kick, land, robot; Player 3: know, dream, tank. Player 3 repeats the word “tank” which was already said, resulting in their disqualification on their third turn.
- Example 2:
- 5 players participate, and the sequence goes smoothly without any repetitions or rule violations, resulting in no disqualifications.
- Example 3:
- With 2 players, Player 1 fails to follow the sequence properly on their third turn by saying “now,” which does not start with the required letter from the previous word, leading to their disqualification.
How to Approach?
step1.
Code
|
|
Table of Contents
Related Posts
tuple
Problem A “tuple” is defined as an ordered collection of elements which can include duplicate values and each element is assigned an order. A tuple with n elements (a1, a2, a3, ..., an) can be expressed using the set notation in various ways, with any ordering of the subsets as:
2024-5-1
braket rotating
Problem Define a string as a “valid bracket string” if it follows the given rules:
“()”, “[]”, and “{}” are all valid bracket strings. If A is a valid bracket string, then “(A)”, “[A]”, and “{A}” are also valid bracket strings. For example, since “[]” is a valid bracket string, “([])” is also valid.
2024-4-27
Tournament
Problem In the Jay’s game tournament, N players compete in a knockout format. Each player is sequentially assigned a number from 1 to N. The matches are arranged as 1 vs 2, 3 vs 4, …, N-1 vs N. The winner of each game advances to the next round, where they receive new sequential numbers starting from 1 for the advancing players.
2024-4-25
High Score Kit: Greedy#1
Problem Given a number, your task is to remove k digits from the number in such a way that the remaining digits form the largest possible number. The number is provided as a string number and the integer k represents the number of digits to remove. Your function solution should return the resulting largest number as a string.
2024-4-11
High Score Kit: DFS&BFS#2
Problem In the ROR game, you play on a team that competes to destroy the opposing team’s base first, making it advantageous to reach the enemy base as quickly as possible. Suppose you’re a team member and are located at position (row: 1, column: 1) on a 5x5 map, while the enemy base is at (row: 5, column: 5).
2024-4-11
High Score Kit: DFS&BFS#1
Problem You are given an array of non-negative integers and a target number. Your task is to find how many ways you can add and subtract the given numbers to achieve the target number. The order of numbers in the array must be preserved. For example, with the array [1, 1, 1, 1, 1], there are five ways to reach the target number 3:
2024-4-11
Sponsor
Wechat
Alipay