Algorithm
Shortest Distance to Target String in a Circular Array
Solution
class Solution:
def closestTarget(self, words: List[str], target: str, startIndex: int) -> int:
ans = n = len(words)
for i, word in enumerate(words):
if word == target:
ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))
return ans if ans < n else -1Video GuideLeetcode Daily
Time Complexity
O(nL)
Space Complexity
O(1)
