mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
52 lines
2.5 KiB
HTML
52 lines
2.5 KiB
HTML
<p>You are given a <strong>0-indexed</strong> <strong>circular</strong> string array <code>words</code> and a string <code>target</code>. A <strong>circular array</strong> means that the array's end connects to the array's beginning.</p>
|
|
|
|
<ul>
|
|
<li>Formally, the next element of <code>words[i]</code> is <code>words[(i + 1) % n]</code> and the previous element of <code>words[i]</code> is <code>words[(i - 1 + n) % n]</code>, where <code>n</code> is the length of <code>words</code>.</li>
|
|
</ul>
|
|
|
|
<p>Starting from <code>startIndex</code>, you can move to either the next word or the previous word with <code>1</code> step at a time.</p>
|
|
|
|
<p>Return <em>the <strong>shortest</strong> distance needed to reach the string</em> <code>target</code>. If the string <code>target</code> does not exist in <code>words</code>, return <code>-1</code>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
|
|
<strong>Output:</strong> 1
|
|
<strong>Explanation:</strong> 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.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
|
|
<strong>Output:</strong> 1
|
|
<strong>Explanation:</strong> We start from index 0 and can reach "leetcode" by
|
|
- moving 2 units to the right to reach index 3.
|
|
- moving 1 unit to the left to reach index 3.
|
|
The shortest distance to reach "leetcode" is 1.</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> words = ["i","eat","leetcode"], target = "ate", startIndex = 0
|
|
<strong>Output:</strong> -1
|
|
<strong>Explanation:</strong> Since "ate" does not exist in <code>words</code>, we return -1.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= words.length <= 100</code></li>
|
|
<li><code>1 <= words[i].length <= 100</code></li>
|
|
<li><code>words[i]</code> and <code>target</code> consist of only lowercase English letters.</li>
|
|
<li><code>0 <= startIndex < words.length</code></li>
|
|
</ul>
|