leetcode1160

 1 class Solution:
 2     def countCharacters(self, words: List[str], chars: str) -> int:
 3         n = len(chars)
 4         m = len(words)
 5         if n == 0 or m == 0:
 6             return 0
 7         dic = {}
 8         
 9         for i in range(n):
10             if chars[i] not in dic:
11                 dic[chars[i]] = 1
12             else:
13                 dic[chars[i]] += 1
14         sums = 0
15         
16         for i in range(m):
17             temp = dic.copy()
18             cur = words[i]
19             k = 0
20             for j in range(len(cur)):
21                 c = cur[j]
22                 if c in temp and temp[c] > 0:
23                     temp[c] -= 1
24                     k += 1
25             if k == len(cur):
26                 sums += len(cur)
27         return sums
28             
29             

算法思路:hash表。

转载于:https://www.cnblogs.com/asenyang/p/11371986.html

你可能感兴趣的:(leetcode1160)