LeetCode solutions

2476. Check Distances Between Same Letters

My accepted Python solution to LeetCode problem 2476, Check Distances Between Same Letters, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.3MB, accepted 2026-01-01.

python
class Solution:
    def checkDistances(self, s: str, distance: List[int]) -> bool:
        first_pos = {}
        for i, c in enumerate(s):
            if c in first_pos:
                actual_dist = i - first_pos[c] - 1
                expected_dist = distance[ord(c) - ord('a')]
                if actual_dist != expected_dist:
                    return False
            else:
                first_pos[c] = i
        return True

Source