2515. Shortest Distance to Target String in a Circular Array LeetCode Solution

In this guide, you will get 2515. Shortest Distance to Target String in a Circular Array LeetCode Solution with the best time and space complexity. The solution to Shortest Distance to Target String in a Circular Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Shortest Distance to Target String in a Circular Array solution in C++
  4. Shortest Distance to Target String in a Circular Array solution in Java
  5. Shortest Distance to Target String in a Circular Array solution in Python
  6. Additional Resources
2515. Shortest Distance to Target String in a Circular Array LeetCode Solution image

Problem Statement of Shortest Distance to Target String in a Circular Array

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.

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.
Return 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”,”leetcode”,”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.

See also  1716. Calculate Money in Leetcode Bank LeetCode Solution

Example 2:

Input: words = [“a”,”b”,”leetcode”], target = “leetcode”, startIndex = 0
Output: 1
Explanation: 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.
Example 3:

Input: words = [“i”,”eat”,”leetcode”], 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2515. Shortest Distance to Target String in a Circular Array LeetCode Solution in C++

class Solution {
 public:
  int closetTarget(vector<string>& words, string target, int startIndex) {
    const int n = words.size();

    for (int i = 0; i < n; ++i) {
      if (words[(startIndex + i + n) % n] == target)
        return i;
      if (words[(startIndex - i + n) % n] == target)
        return i;
    }

    return -1;
  }
};
/* code provided by PROGIEZ */

2515. Shortest Distance to Target String in a Circular Array LeetCode Solution in Java

class Solution {
  public int closetTarget(String[] words, String target, int startIndex) {
    final int n = words.length;

    for (int i = 0; i < n; ++i) {
      if (words[(startIndex + i + n) % n].equals(target))
        return i;
      if (words[(startIndex - i + n) % n].equals(target))
        return i;
    }

    return -1;
  }
}
// code provided by PROGIEZ

2515. Shortest Distance to Target String in a Circular Array LeetCode Solution in Python

class Solution:
  def closetTarget(self, words: list[str], target: str, startIndex: int) -> int:
    n = len(words)

    for i in range(n):
      if words[(startIndex + i + n) % n] == target:
        return i
      if words[(startIndex - i + n) % n] == target:
        return i

    return -1
# code by PROGIEZ

Additional Resources

See also  3194. Minimum Average of Smallest and Largest Elements LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.