PracticeSearching

First Combo in the Move Code

MediumSearchingtraversalstate trackingprefix reasoning Time not estimated Not started

Context

A game records a player's moves as one lowercase string. A rival wants to know where a particular combo first appears in that record. Return the requested combo together with its first start and exclusive end positions. If the combo never appears, report the combo with -1 for both positions.

Problem

Implement a function that searches the move-code string for the first literal occurrence of the requested combo. Return a three-item string list: the requested combo, its inclusive starting index, and its exclusive ending index, with both indices written as decimal strings. Search from the beginning and select the earliest occurrence. If no occurrence exists, return the requested combo followed by "-1" and "-1". Do not alter either input string.

Examples

Example 1
Input: combo = "aba"move_code = "abacabad"
Output: ["aba", "0", "3"]
Explanation: For move_code "abacabad" and combo "aba", the executed result is ["aba", "0", "3"].
Example 2
Input: combo = "down"move_code = "leftupdownright"
Output: ["down", "6", "10"]
Explanation: For move_code "leftupdownright" and combo "down", the executed result is ["down", "6", "10"].
Example 3
Input: combo = "spin"move_code = "jumpdashroll"
Output: ["spin", "-1", "-1"]
Explanation: For move_code "jumpdashroll" and combo "spin", the executed result is ["spin", "-1", "-1"].

Constraints

  • 1 <= len(move_code) <= 240
  • move_code: alphabet lowercase
  • 1 <= len(combo) <= len(move_code)
  • combo: alphabet lowercase
  • move_code and combo contain only lowercase letters.
  • Types: move_code is str, combo is str; result is str[]

Function signature

def find_first_combo(move_code: str, combo: str) -> list[str]
move_code str
The recorded lowercase move sequence to search.
combo str
The lowercase move pattern to find.
returns list[str]
A three-item list containing combo, its first-match start index as a string, and its exclusive end index as a string; use "-1" for both indices when absent.

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

Notes

  • Indices are zero-based.
  • The ending index is exclusive.
  • The returned combo must remain unchanged even when no match exists.
Approved · cb051-abs_7_v214 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1