[leetcode-438-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.
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".
思路:
用一个滑动窗口,随时判断窗口里面的字符是否与p字符串相符合anagram。
vector<int> findAnagrams(string s, string p) {
vector<int> pv(,), sv(,), res;
if(s.size() < p.size())
return res;
for(int i = ; i < p.size(); ++i)
{
++pv[p[i]];
++sv[s[i]];
}
if(pv == sv)
res.push_back();
for(int i = p.size(); i < s.size(); ++i)
{
++sv[s[i]];
--sv[s[i-p.size()]];
if(pv == sv)
res.push_back(i-p.size()+);
}
return res;
}
参考:
https://discuss.leetcode.com/topic/64390/c-o-n-sliding-window-concise-solution-with-explanation
[leetcode-438-Find All Anagrams in a String]的更多相关文章
- [LeetCode] 438. 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 ...
- LeetCode 438. 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 ...
- [leetcode]438. 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 ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- [LeetCode] 438. Find All Anagrams in a String_Easy
438. Find All Anagrams in a String DescriptionHintsSubmissionsDiscussSolution Pick One Given a str ...
- 【LeetCode】438. Find All Anagrams in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 双指针 日期 题目地址:https://l ...
- 【leetcode❤python】 438. Find All Anagrams in a String
class Solution(object): def findAnagrams(self, s, p): """ :type s: s ...
随机推荐
- MySQL监听数据库存储过程出现异常
DELIMITER $$ DROP PROCEDURE IF EXISTS `proc_ordertourist_cancel`$$ CREATE PROCEDURE proc_ordertouris ...
- 简单jQuery 实现手风琴菜单
简单jQuery 实现手风琴菜单 css代码 如下: *{ margin: 0; padding: 0; } #accordion{ width: 500px; } #accordion>div ...
- python小工具:用python操作HP的Quality Center
背景是这样的:这个组的测试人员每跑一个case都要上传测试结果附件到QC.每个待测功能模块可能包含几十上百的case.于是手工上传测试结果变成了繁重的体力劳动.令人惊讶的是我们的工具开发组竟然说做不了 ...
- 【方法】Html5实现文件异步上传
1 简介 开发文件上传功能从来不是一件愉快的事,异步上传更是如此,使用过iframe和Flash的上传方案,也都感觉十分的别扭.本文简要简绍利用Html5的FormData实现文件的异步上传,还可以实 ...
- Ubuntu 挂载硬盘分区
1.先查看当前硬盘分区状态,命令sudo fdisk -l 大致如下:设备 启动 Start 末尾 扇区 Size Id 类型/dev/sda1 2048 206847 204800 100M 7 H ...
- python csv例子
import csv fieldnames = ['Column1', 'Column2', 'Column3', 'Column4'] rows = [{'Column1': '0', 'Colum ...
- centos7搭建SVN+Apache+IF.svnadmin实现web管理SVN
阅读目录 1. 介绍 2. 软件准备 3. 建立SVN Server仓库 4. 配置安装PHP&IF.SVNadmin 5. 启动服务 1.介绍 公司最近想把Windows server平台的 ...
- 对clear float 的理解
之前自己对于清除浮动的用法比较模糊 ,如果用到的话,一般都是采用简单粗暴的方式解决,就是直接用overflow:hidden,但是越用久就会发现其实有BUG,这个BUG正是overflow:hidde ...
- springcloud(七):配置中心svn示例和refresh
上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...
- Sampling Distributions and Central Limit Theorem in R(转)
The Central Limit Theorem (CLT), and the concept of the sampling distribution, are critical for unde ...