Re: [闲聊] 每日leetcode

楼主: dont   2024-08-05 14:56:48
2053. Kth Distinct String in an Array
## 思路
用Counter计算string出现次数
再for loop找第k个Counter[s] == 1 的字串
## Complexity
Time: O(N)
Space: O(N)
## Code
```python
class Solution:
def kthDistinct(self, arr: List[str], k: int) -> str:
counter = Counter(arr)
for s in arr:
if counter[s] == 1:
k -= 1
if k == 0:
return s
return ''
```

Links booklink

Contact Us: admin [ a t ] ucptt.com