Skip to content

Practice Shortest Distance to Target String in a Circular Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2515
LeetCode Problem

Shortest Distance to Target String in a Circular Array

Problem Statement

You are given 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. Starting from startIndex, you can move to either the next word or the previous word with 1 step at a time. Return the shortest distance needed to reach the string target. If the string target does not exist in words, return -1.

Source: Shortest Distance to Target String in a Circular Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
Output
1

# Example 2

Input
words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
Output
1

# Example 3

Input
words = ["i","eat","leetcode"], target = "ate", startIndex = 0
Output
-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

Related LeetCode Problems

Practice more problems similar to Shortest Distance to Target String in a Circular Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.