---
# System prepended metadata

title: 2515. Shortest Distance to Target String in a Circular Array
tags: [Leetcode, Easy]

---

# 2515. Shortest Distance to Target String in a Circular Array
###### tags: `Leetcode` `Easy`
Link: https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/description/
## Code
java
```java=
class Solution {
    public int closetTarget(String[] words, String target, int startIndex) {
        int res = Integer.MAX_VALUE;
        int n = words.length;
        for(int i=0; i<n; i++){
            if(words[i].equals(target)){
                int d = Math.abs(i-startIndex);
                res = Math.min(res, d);
                res = Math.min(res, n-d);
            }
        }
        return res==Integer.MAX_VALUE?-1:res;
    }
}
```
python
```python=
class Solution:
    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:
        n = len(words)
        ans = n
        for i in range(n):
            if words[i]==target:
                d = abs(i-startIndex)
                ans = min(ans, d)
                ans = min(ans, n-d)
        return -1 if ans==n else ans
```