题目:给定由字母组成的字符串s1和s2,其中,s2中字母的个数少于s1,如何判断s1是否包含s2?
分析:哈希法。
code:
str1 = 'aaaabbce'
str2 = 'abcbbaaad'
list1 = list(str1)
list2 = list(str2)
i = 0
hashTable1 = dict()
while i < len(str1):
if list1[i] not in hashTable1:
hashTable1[list1[i]] = 0
i += 1
i = 0
hashTable2 = dict()
while i < len(str2):
if list2[i] not in hashTable2:
hashTable2[list2[i]] = 0
i += 1
count = 0
for k, v in hashTable1.items():
if k in hashTable2:
count += 1
else:
print("不包含")
break
程序运行结果:
不包含
网友评论