-
Notifications
You must be signed in to change notification settings - Fork 0
/
2053.py
46 lines (31 loc) · 1.06 KB
/
2053.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from typing import List
import unittest
from collections import defaultdict
class Solution:
def kthDistinct(self, arr: List[str], k: int) -> str:
count = 0
table = defaultdict(lambda: 0)
for string in arr:
table[string] += 1
for string in arr:
count += 1
if table[string] > 1:
count -= 1
if count == k:
return string
return ""
class Test(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_first(self):
self.assertEqual(
self.solution.kthDistinct(arr=["d", "b", "c", "b", "c", "a"], k=2), "a"
)
def test_second(self):
self.assertEqual(self.solution.kthDistinct(arr=["aaa", "aa", "a"], k=1), "aaa")
def test_third(self):
self.assertEqual(self.solution.kthDistinct(arr=["a", "b", "a"], k=3), "")
def test_fourth(self):
self.assertEqual(self.solution.kthDistinct(arr=["a", "a", "b", "a"], k=1), "b")
if __name__ == '__main__':
unittest.main()