-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython10.py
More file actions
38 lines (36 loc) · 1.11 KB
/
Python10.py
File metadata and controls
38 lines (36 loc) · 1.11 KB
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
"""
赎金信:
法一:
使用哈希表储存杂志中的字符串,并记录个数
在赎金信中查找存在的字符串,找到一个个数减一
ransomNote ="aa"
magazine = "aab"
dict = {}
a = True
for i in range(len(magazine)):
if magazine[i] in dict:
dict[magazine[i]] += 1
else:
dict[magazine[i]] = 1
for i in range(len(ransomNote)):
if ransomNote[i] in dict and dict[ransomNote[i]] != 0:
dict[ransomNote[i]] -= 1
else:
a = False
print(a)
法二(究极简单):
使用python内部自带的Counter类
将两个字符串储存在counter类中
利用其自带的交集查找方法,返回对应布尔值
但是注意,使用Counter类前应导入
From collection import Counter
"""
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
a = Counter(ransomNote)
b = Counter(magazine)
if (a & b) == a:
return True
else:
return False
#更简单的写法 return (a & b) == a