博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode-438-Find All Anagrams in a String
阅读量:7090 次
发布时间:2019-06-28

本文共 1310 字,大约阅读时间需要 4 分钟。

题目描述:

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.

Example 1:

Input:s: "cbaebabacd" p: "abc"Output:[0, 6]Explanation:The substring with start index = 0 is "cba", which is an anagram of "abc".The substring with start index = 6 is "bac", which is an anagram of "abc".

 

Example 2:

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".

要完成的函数:

vector<int> findAnagrams(string s, string p) 

说明:

1、给定一个字符串s和非空字符串p,将p中元素不断交换形成一个新的字符串,如果这个新的字符串在s中能找到匹配的,那么就输出匹配到的开始的位置,直到处理到字符串s结束。

2、这道题目难道要记住p经过交换可能形成的所有字符串吗,难道再类似于滑动窗口一般不断在s中比较?

其实不用记住所有字符串,记住p经过交换可能形成的所有字符串其实等价于记住p中所有字母出现的次数。

所以代码如下:

vector
findAnagrams(string s, string p) { vector
res; vector
p1(26,0); vector
s1(26,0); for(int i=0;i

上述代码实测35ms,beats 90.84% of cpp submissions。

 

转载于:https://www.cnblogs.com/chenjx85/p/8906022.html

你可能感兴趣的文章
一对多的两个表,查询主表的信息和主表在子表中的记录条数
查看>>
从程序员入门到“第一个项目”的一些事
查看>>
转-Pentaho技术白皮书中文版(三)--构建新组件
查看>>
SpringSrcureCode在grails中实现用户--角色--权限的管理
查看>>
java Servlet 下载 itext 生成的2003 word 文档(java生成word文档3)
查看>>
Delphi 查找标题已知的窗口句柄,遍历窗口控件句柄(转)
查看>>
单例模式
查看>>
最锋利的jQuery源码、电子书及视频教程合集(共46个)
查看>>
JavaScript 内置对象!
查看>>
解决ubuntu下打不开rar文件
查看>>
内核启动过程
查看>>
在使用ibatis实现多条件模糊查询的语句
查看>>
童宁_下一代数据中心的安全挑战
查看>>
android 3g状态及信号监测
查看>>
开源 java CMS - FreeCMS2.8 站点管理
查看>>
JSP中include指令和include行为区别
查看>>
关于zend studio 9.0版本汉化
查看>>
java web编程学习6
查看>>
CSS学习笔记——最基础的定义与使用
查看>>
形象的网络状况工具
查看>>