美文网首页【python程序员面试宝典|程序员算法宝典】
【python】判断两个字符串的包含关系?

【python】判断两个字符串的包含关系?

作者: 阿牛02 | 来源:发表于2019-07-25 11:41 被阅读0次

题目:给定由字母组成的字符串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

程序运行结果:
不包含

相关文章

网友评论

    本文标题:【python】判断两个字符串的包含关系?

    本文链接:https://www.haomeiwen.com/subject/slxxrctx.html