2598. Shortest Distance to Target String in a Circular Array
My accepted Python solution to LeetCode problem 2598, Shortest Distance to Target String in a Circular Array, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.3MB, accepted 2026-01-01.
class Solution:
def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
n = len(words)
if target not in words:
return -1
min_dist = float('inf')
for i in range(n):
if words[i] == target:
forward = (i - startIndex) % n
backward = (startIndex - i) % n
min_dist = min(min_dist, forward, backward)
return min_dist
def closestTarget(self, words: List[str], target: str, startIndex: int) -> int:
return self.closetTarget(words, target, startIndex)