Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance to Target String in a Circular Array

Shortest Distance to Target String in a Circular Array asks for the fewest moves, left or right around the array, from a starting index to any target occurrence, or -1 if none exists.

Statement

You are given a circular array of strings words and a string target. You are also given an integer startIndex representing your starting position in words.

In a circular array, moving right from the last index leads to the first index, and moving left from the first index leads to the last index.

Starting from startIndex, you can move either left or right by 11 step at a time.

Return the shortest distance needed to reach any index where the string is equal to target. If target does not exist in words, return −1-1.

Constraints:

  • 1≤1 \leq words.length ≤100\leq 100

  • 1≤1 \leq words[i].length ≤100\leq 100

  • words[i] and target consist of only lowercase English letters.

  • 0≤0 \leq startIndex << words.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance to Target String in a Circular Array

Shortest Distance to Target String in a Circular Array asks for the fewest moves, left or right around the array, from a starting index to any target occurrence, or -1 if none exists.

Statement

You are given a circular array of strings words and a string target. You are also given an integer startIndex representing your starting position in words.

In a circular array, moving right from the last index leads to the first index, and moving left from the first index leads to the last index.

Starting from startIndex, you can move either left or right by 11 step at a time.

Return the shortest distance needed to reach any index where the string is equal to target. If target does not exist in words, return −1-1.

Constraints:

  • 1≤1 \leq words.length ≤100\leq 100

  • 1≤1 \leq words[i].length ≤100\leq 100

  • words[i] and target consist of only lowercase English letters.

  • 0≤0 \leq startIndex << words.length