Skip to main content
impol.dev

Shortest Distance to Target String in a Circular Array

Easy

You receive a 0-indexed circular string array words and a string target . A circular array means that the array's end connects to the array's beginning.

  • Formally, the next element of words[i] is words[(i + 1) % n] and the previous element of words[i] is words[(i - 1 + n) % n] , where n is the length of words .

Starting from startIndex , you can move to either the next word or the previous word with 1 step at a time.

Produce the shortest distance needed to reach the string target . If the string target does not exist in words , return -1 .

Example 1

Input: words = ["hello","i","am","impoldev","hello"], target = "hello", startIndex = 1

Output: 1

Explanation: We start from index 1 and can reach "hello" by

  • moving 3 units to the right to reach index 4.

  • moving 2 units to the left to reach index 4.

  • moving 4 units to the right to reach index 0.

  • moving 1 unit to the left to reach index 0.

The shortest distance to reach "hello" is 1.

Example 2

Input: words = ["a","b","impoldev"], target = "impoldev", startIndex = 0

Output: 1

Explanation: We start from index 0 and can reach "impoldev" by

  • moving 2 units to the right to reach index 2.

  • moving 1 unit to the left to reach index 2.

The shortest distance to reach "impoldev" is 1.

Example 3

Input: words = ["i","eat","impoldev"], target = "ate", startIndex = 0

Output: -1

Explanation: Since "ate" does not exist in words, we return -1.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] and target consist of only lowercase English letters.
  • 0 <= startIndex < words.length

Did this problem appear in one of your interviews?

Responses stay private.

Acceptance rate: 0%

© 2026 impol.dev. All rights reserved.PrivacyTerms