码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode1160

时间:2019-08-18 13:28:20      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:ret   elf   code   self   +=   solution   div   else   class   

 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表。

leetcode1160

标签:ret   elf   code   self   +=   solution   div   else   class   

原文地址:https://www.cnblogs.com/asenyang/p/11371986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!