File: shortest-distance-to-target-string-in-a-circular-array/solution.py

Date: 2026-06-06

Time: 19:05

Purpose

This file solves LeetCode 2515: Shortest Distance to Target String in a Circular Array. It provides a single function shortest_distance that finds the minimum number of steps to reach a target string from a given starting index, where the array wraps around circularly (you can walk left or right).

Key Components

shortest_distance(words, target, startIndex) -> int

The sole public function. Given a circular array of strings, it returns the fewest steps needed to reach any occurrence of target starting from startIndex, moving either direction. Returns -1 if target isn't present.

Parameters:

Return: minimum circular distance, or -1

Patterns

Sentinel-based minimum tracking. Instead of tracking a boolean found flag separately, the code initializes result = n — a value impossible to achieve as a real distance (max circular distance is n // 2). After the loop, result < n doubles as a "was found" check. This avoids branching on a separate flag.

Dual-distance formula. For any candidate index i, the clockwise distance is abs(i - startIndex) and the counter-clockwise distance is n - abs(i - startIndex). The line min(result, dist, n - dist) computes both directions in one expression and keeps the running minimum — a standard idiom for circular distance problems.

Dependencies

Imports: None. Pure function with no external dependencies.

Imported by: Its own test_solution.py. The "Imported By" list in the prompt is misleading — those are test files for *other* problems that happen to share a test harness or runner pattern, not actual imports of this function.

Flow

1. Capture array length n.

2. Set sentinel result = n.

3. Linear scan: for each index i, if words[i] == target, compute dist = abs(i - startIndex), then update result = min(result, dist, n - dist).

4. Return result if a match was found (result < n), else -1.

Single pass, O(n) time, O(1) space.

Invariants

Error Handling

No explicit error handling. If target is absent from words, the sentinel survives the loop and the ternary returns -1. Empty words list would return -1 correctly (loop body never executes, result stays at 0 which equals n... actually both are 0, so 0 < 0 is false → returns -1).

Topics to Explore

Beliefs