Skip to content
LC-2515 Easy LeetCode

2515. Shortest Distance to Target String in a Circular Array

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 50% Topics: Array, String
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# array
class Solution(object):
    def closetTarget(self, words, target, startIndex):
        """
        :type words: List[str]
        :type target: str
        :type startIndex: int
        :rtype: int
        """
        INF = float("inf")
        result = INF
        for i, w in enumerate(words):
            if w == target:
                result = min(result, (i-startIndex)%len(words), (startIndex-i)%len(words))
        return result if result != INF else -1

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions