leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 06355bf91a27fdec5548fed0f92083a447fcee35
parent 7d4c4eb05eb2ff3fd292b8afff686516f301ca7f
Author: Jack Sangdahl <jack@pngu.org>
Date:   Tue, 14 Apr 2026 19:50:27 -0600

2515. shortest distance to target string in a circular array

Diffstat:
Ashortest_dist_target_str.c | 36++++++++++++++++++++++++++++++++++++
Ashortest_dist_target_str.py | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/shortest_dist_target_str.c b/shortest_dist_target_str.c @@ -0,0 +1,36 @@ +// 2515. Shortest Distance to Target String in a Circular Array +#include "leetcode.h" + +/* + * 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. 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. + */ + +int closestTarget(char **words, int wordsSize, char *target, int startIndex) { + int ans = wordsSize, n = wordsSize; + for (int i = 0; i < wordsSize; i++) + if (!strcmp(words[i], target)) { + int dist = abs(i - startIndex); + ans = fmin(ans, fmin(dist, n - dist)); + } + return ans < n ? ans : -1; +} + +int main() { + char *w1[] = {"hello", "i", "am", "leetcode", "hello"}, *t1 = "hello"; + char *w2[] = {"a", "b", "leetcode"}, *t2 = "leetcode"; + char *w3[] = {"i", "eat", "leetcode"}, *t3 = "ate"; + int r1 = closestTarget(w1, ARRAY_SIZE(w1), t1, 1); + int r2 = closestTarget(w2, ARRAY_SIZE(w2), t2, 0); + int r3 = closestTarget(w3, ARRAY_SIZE(w3), t3, 0); + printf("%d\n", r1); + assert(r1 == 1); + printf("%d\n", r2); + assert(r2 == 1); + printf("%d\n", r3); + assert(r3 == -1); +} diff --git a/shortest_dist_target_str.py b/shortest_dist_target_str.py @@ -0,0 +1,40 @@ +# 2515. 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. 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. +""" + + +class Solution(object): + def closestTarget(self, words, target, startIndex): + """ + :type words: List[str] + :type target: str + :type startIndex: int + :rtype: int + """ + ans, n = len(words), len(words) + for i, j in enumerate(words): + if j == target: + ans = min(ans, abs(i - startIndex), n - abs(i - startIndex)) + return ans if ans < n else -1 + + +if __name__ == "__main__": + obj = Solution() + print( + obj.closestTarget( + words=["hello", "i", "am", "leetcode", "hello"], + target="hello", + startIndex=1, + ) + ) + print( + obj.closestTarget(words=["a", "b", "leetcode"], target="leetcode", startIndex=0) + ) + print(obj.closestTarget(words=["i", "eat", "leetcode"], target="ate", startIndex=0))