Advertisement
2515. Shortest Distance to Target String in a Circular Array
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Approach
Expand outward from startIndex by distance i and check both clockwise and counterclockwise positions in the circular array. The first match gives the minimum distance by construction.
2515.cs
C#
// Approach: Expand outward from startIndex by distance i and check both clockwise and
// counterclockwise positions in the circular array. The first match gives the minimum
// distance by construction.
// Time: O(n) Space: O(1)
public class Solution
{
public int ClosestTarget(string[] words, string target, int startIndex)
{
int n = words.Length;
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;
}
}
Advertisement
Was this solution helpful?