-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1002.py
46 lines (36 loc) · 1.51 KB
/
p1002.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
# https://leetcode.cn/problems/find-common-characters/description/
import unittest
from typing import List
class Solution:
@staticmethod
def commonChars(words: List[str]) -> List[str]:
from string import ascii_lowercase
# 构建每个单词的字典
# bella -> [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# label -> [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# roller -> [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]
# ans -> e l*2
cnt: list[list[int]] = []
for word in words:
tmp: list[int] = [0] * 26
for c in word:
tmp[ord(c) - ord("a")] += 1
cnt.append(tmp)
ans: list[str] = []
for j in range(26):
# 矩阵第j列的最小值,即字符的重复次数
rpt: int = len(words[0])
for i in range(len(words)):
rpt = min(rpt, cnt[i][j])
# 添加答案,将第j个字符重复rpt次
for _ in range(rpt):
ans.append(ascii_lowercase[j])
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(
Solution.commonChars(["bella", "label", "roller"]), ["e", "l", "l"]
)
self.assertEqual(Solution.commonChars(["cool", "lock", "cook"]), ["c", "o"])
if __name__ == "__main__":
unittest.main()