找出字符串里所有的Anagrams(Find All Anagrams in a String)

欢迎关注本人博客:云端筑梦师

题目:

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.

题意为给定一个字符串s和一个非空字符串p,找出s中所有是p的anagrams的子串的起始索引,所有字符串均由小写字母组成,且字符串长度不超过20100,将结果以列表形式返回,不必考虑列表的顺序。感兴趣的童鞋可以戳这里看原题

例如:

Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

算法:

先上代码,这里用python3实现:

class Solution:
    def findAnagrams(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: List[int]
        """
        res=[]
        m,n=len(s),len(p)
        phash=[0]*123
        shash=[0]*123
        if n>m:return res
        for i in p:
            phash[ord(i)]+=1
        for i in s[:n-1]:
            shash[ord(i)]+=1
        for i in range(n-1,m):
            shash[ord(s[i])]+=1
            if i-n>=0:
                shash[ord(s[i-n])]-=1
            if shash==phash:
                res.append(i-n+1)
        return res

思路是这样的,考虑到字符串由小写字母组成,所以可利用哈希表和ascii编码,字母的编码范围是65~122,所以可以创建连个长度为123键值全为0的哈希表,先对p遍历,将p中出现的字母及次数用哈希表phash记录下来,再依次遍历s的每个字母并记录到shash,并与phash进行比对,若相等,则说明找到了一个anagrams,将此时的起始索引存进res列表即可,直到s遍历完,返回res。

转载于:https://www.cnblogs.com/Aurora-Twinkle/p/8688129.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值