PracticeStrings

First Winning Token Pair

EasyStringstraversalparsingstate tracking Time not estimated Not started

Context

In the game Starforge, the board records short event messages as players make moves. A bonus rule activates when one event contains two qualifying word tokens. The qualifying tokens must both start with the same badge prefix, and the first event in board order wins. Report the two tokens that activate the rule.

Problem

Implement a function that scans the event messages from left to right. In each message, identify tokens as maximal consecutive runs of lowercase English letters; every other character is a separator. Find the first message containing two token occurrences, in left-to-right order, that both begin with the supplied prefix. Return those two token strings in their original order. Repeated text is allowed, and the same token value may be returned twice when it appears in two separate occurrences. Stop considering later messages as soon as the first valid pair is found. If no message contains two qualifying tokens, return an empty list.

Examples

Example 1
Input: events = ["alpha beta gamma"]prefix = "a"
Output: []
Explanation: The executed result is [] for the event "alpha beta gamma" with prefix "a".
Example 2
Input: events = ["red-blue signal", "start stone star"]prefix = "st"
Output: ["start", "stone"]
Explanation: The executed result is ["start", "stone"] because the first qualifying pair is found in the second event.
Example 3
Input: events = ["quiet river", "one bright moon"]prefix = "z"
Output: []
Explanation: The executed result is [] because neither event produces two qualifying tokens for prefix "z".

Constraints

  • 0 <= len(events) <= 100
  • 0 <= len(events[i]) <= 200 for every i
  • 1 <= len(prefix) <= 20
  • prefix: alphabet lowercase
  • A token is a maximal run of lowercase English letters.
  • Any character other than a lowercase English letter separates neighboring tokens
  • event messages may be empty.
  • Types: events is str[], prefix is str; result is str[]

Function signature

def first_winning_token_pair(events: list[str], prefix: str) -> list[str]
events list[str]
Board event messages scanned from earliest to latest.
prefix str
The lowercase prefix that each qualifying token must start with.
returns list[str]
The first qualifying pair of token occurrences in scan order, or an empty list when no event contains two such tokens.

Adapted from MBPP problem task_305 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Notes

  • Token occurrences count separately, so duplicate qualifying words can form the pair.
  • The pair must come from one event, and events are prioritized by their input order.
Approved · c008r1-abs_759_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1