Leetcode 2399. Check Distances Between Same Letters

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Check Distances Between Same Letters

2. Solution

解析:Version 1,统计字符串中两个字符的距离,当统计到第二个字符计算出距离时,检查对应位置的distance是否与统计的一致。

  • Version 1
1
2
3
4
5
6
7
8
9
10
11
class Solution:
def checkDistances(self, s: str, distance: List[int]) -> bool:
stat = {}
for i, ch in enumerate(s):
if ch not in stat:
stat[ch] = i
else:
stat[ch] = i - stat[ch] - 1
if stat[ch] != distance[ord(ch) - ord('a')]:
return False
return True

Reference

  1. https://leetcode.com/problems/check-distances-between-same-letters/
如果有收获,可以请我喝杯咖啡!