2515. Shortest Distance to Target String in a Circular Array Easy
1/**
2 * [2515] Shortest Distance to Target String in a Circular Array
3 *
4 * 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.
5 *
6 * 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.
7 *
8 * Starting from startIndex, you can move to either the next word or the previous word with 1 step at a time.
9 * Return the shortest distance needed to reach the string target. If the string target does not exist in words, return -1.
10 *
11 * <strong class="example">Example 1:
12 *
13 * Input: words = ["hello","i","am","leetcode","hello"], target = "hello", startIndex = 1
14 * Output: 1
15 * Explanation: We start from index 1 and can reach "hello" by
16 * - moving 3 units to the right to reach index 4.
17 * - moving 2 units to the left to reach index 4.
18 * - moving 4 units to the right to reach index 0.
19 * - moving 1 unit to the left to reach index 0.
20 * The shortest distance to reach "hello" is 1.
21 *
22 * <strong class="example">Example 2:
23 *
24 * Input: words = ["a","b","leetcode"], target = "leetcode", startIndex = 0
25 * Output: 1
26 * Explanation: We start from index 0 and can reach "leetcode" by
27 * - moving 2 units to the right to reach index 3.
28 * - moving 1 unit to the left to reach index 3.
29 * The shortest distance to reach "leetcode" is 1.
30 * <strong class="example">Example 3:
31 *
32 * Input: words = ["i","eat","leetcode"], target = "ate", startIndex = 0
33 * Output: -1
34 * Explanation: Since "ate" does not exist in words, we return -1.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= words.length <= 100
40 * 1 <= words[i].length <= 100
41 * words[i] and target consist of only lowercase English letters.
42 * 0 <= startIndex < words.length
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/
48// discuss: https://leetcode.com/problems/shortest-distance-to-target-string-in-a-circular-array/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn closet_target(words: Vec<String>, target: String, start_index: i32) -> i32 {
54 0
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_2515() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.