Find All Anagrams in a String 找变位子串:

本题难度为easy  当看到题目的第一眼 有些怀疑  怎么能等级那么简单  通过网上找到思路后发现 确实不难  现整理总结思路

原字符串s,在s中寻找字符串p的变位字符串的位置,并依次输出,且字符串元素均为小写字母。

思路:首先题目中给定 只有小写字母 所以可以用一个数组长度26来包含完所有的元素情况 并记录其对应出现次数 当然首先应该记录的应该是p的元素的对应次数pvec

然后再s中,从第一个元素开始往后找 在长度一样时 每次与pvec来比较 与p的元素和所对应的次数是否完全一样 在这个过程中 与顺序无关  且当s中元素往后移动一位

前面起始位置都应该后移一位

过程:

1、记录p中元素及其出现次数 :pvec

2、在s中 从位置0开始依次后移 依次往后记录当前s中元素出现的次数svec

3、在当前s记录过程中 长度达到p的长度时  就要从开始端移除最开始元素了

4、比较svec中元素记录和pvec中是否相等 相等则表示在这一长度中 找到了p的一个变位子串位置 即为s中当前长度的开始位置 并记录下来

5、转第二步 直至s中元素记录完毕

 

代码:

vector<int> findAnagrams(string s, string p) {
    vector<int> res;
    if (s.length() < p.length() || s.length() == 0 || p.length() == 0)
        return res;
    vector<int> svec(26), pvec(26);
   
    for (int i = 0; i < p.length(); ++i)
        pvec[p[i] - 'a']++;
    for (int i = 0; i < s.length(); ++i)
    {
        svec[s[i] - 'a']++;
        if (i >= p.length())
            svec[s[i - p.length()] - 'a']--;
        if (svec == pvec)
            res.push_back(i - p.length()+1);
    }
    return res;
}

     

转载于:https://www.cnblogs.com/weiyi-mgh/p/6291159.html

leetcode 438:Find All Anagrams in a String 找变位子串相关推荐

  1. leetcode 438. Find All Anagrams in a String | 438. 找到字符串中所有字母异位词(Java)

    题目 https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题解 方法1:尝试构造一种"与顺序无关的哈希" 思考 ...

  2. leetcode 438. Find All Anagrams in a String 滑动窗口法

    题目链接 解析 主要使用滑动窗口法解题,需要好好体会的是中间的两个判断couter的用法,这里很巧妙. 如果想了解更多的滑动窗口法内容,看这里: 滑动窗口法详解 代码 from collections ...

  3. LeetCode 438. Find All Anagrams in a String--字符串-滑动窗口--C++,Python解法

    题目地址:Find All Anagrams in a String - LeetCode Given a string s and a non-empty string p, find all th ...

  4. 438. Find All Anagrams in a String

    https://www.cnblogs.com/grandyang/p/6014408.html 用两个hash表来存储个数 class Solution { public:vector<int ...

  5. leetcode算法题--Reverse Words in a String

    原题链接:https://leetcode.com/problems/reverse-words-in-a-string/ class Solution {public:string reverseW ...

  6. LeetCode:96. Unique Binary Search Trees(找出独一无二搜索树)

    文章最前: 我是Octopus,这个名字来源于我的中文名--章鱼:我热爱编程.热爱算法.热爱开源.所有源码在我的个人github :这博客是记录我学习的点点滴滴,如果您对 Python.Java.AI ...

  7. LeetCode Find All Anagrams in a String

    题意:给出两个字符串s,t,找出s中所有的p的打散后的字符串在s中的下标 思路:使用类似队列的思路来做,在针对s元素入队列时,判断元素是否在p中出现.当队列元素个数刚好为p的长度,并且为p的打散字符串 ...

  8. LeetCode: 387. First Unique Character in a String

    051105 题目 Given a string, find the first non-repeating character in it and return it's index. If it ...

  9. LeetCode:345. Reverse Vowels of a String

    051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...

最新文章

  1. mysql使用sha256密码,MySQL5.6启用sha256_password插件
  2. 导入语句 python_Python导入语句说明
  3. PLSQL Developer连接oracle数据库安装及配置
  4. VLAN基础配置及Access接口
  5. Jmeter如何在大并发测试下,让登录或者后续接口只执行一次?
  6. BootStrap中Affix控件的使用方法及如何保持布局的美观
  7. vue返回上级并且携带数据_前后端分离之后端返回用户角色信息(vueelementadmin+laravel)...
  8. 自定义git忽略规则
  9. SqlPlus中退格键和方向键的设置
  10. UWP xaml 圆形头像
  11. opencv4找不到opencv2/core/core.hpp: No such file or directory
  12. 湖畔第一大脑蒋烁淼,爱技术也爱创业,送给正在奋斗的你
  13. 微信生成公众号带参数二维码(一)
  14. 网络术语,接口和协议简介
  15. Cartoon Animator动画制作软件CTA自动保存项目小工具
  16. Nestjs模块机制的概念和实现原理
  17. HTML5语音标签和作用,HTML5标签:source元素的使用方法及作用
  18. 无分类编址(超网)中的网络前缀
  19. z中国禽肉深加工市场盈利动态与竞争前景分析报告2022-2027年
  20. 在Windows上配置Maven环境

热门文章

  1. Python 懂车帝全车系销量排行榜
  2. IE中页面不居中,火狐谷歌等正常
  3. python扩展库安装
  4. oracle 取前10条记录
  5. 第二期冲刺站立会议个人博客15(2016/6/08)
  6. win 7 系统过期处理办法
  7. 使用Keras进行深度学习:(二)CNN讲解及实践
  8. SVG animation 回顾
  9. Java多维数组使用注意事项
  10. ecshop /includes/init.php Arbitrary User Login Vul