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

Subscribe to see which companies asked this question

思路:

运用hash表,用一个hash表h1统计字符串p中的元素,用另一个hash表h2统计字符串s中的元素,设p中的元素个数是m个,则h2只统计连续的m个元素。所以当坐标>=i时,每往后遍历一个元素,先前的元素也要减一个。

代码如下:

 class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int>ans;
int freq[]={};
for(char c:p)
freq[c-'a']++;
for(int l=,r=,cnt=;r<s.size();++r)
{
if(r-l==p.length() && ++freq[s[l++]-'a']>)
cnt--;
if(freq[s[r]-'a']--> && ++cnt==p.length())
ans.push_back(l);
}
return ans;
}
};

【leetcode】Find All Anagrams in a String的更多相关文章

  1. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  2. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  3. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  4. 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...

  5. 【LeetCode】49. Group Anagrams

    题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...

  6. 【LeetCode】839. 相似字符串组 Similar String Groups (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 解题思路 并查集 代码 刷题心得 欢迎 ...

  7. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  8. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  9. 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

随机推荐

  1. 【动态规划】bzoj1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

    思路自然的巧妙dp Description 有N头奶牛,每头那牛都有一个标号Pi,1 <= Pi <= M <= N <= 40000.现在Farmer John要把这些奶牛分 ...

  2. MySQL中一条SQL的加锁分析

    MySQL中一条SQL的加锁分析 id主键 + RC id唯一索引 + RC id非唯一索引 + RC id无索引 + RC id主键 + RR id唯一索引 + RR id非唯一索引 + RR id ...

  3. MyEclipse访问MSSQL2008数据库

    首先到微软网站http://www.microsoft.com/zh-cn/download/details.aspx?id=21599下载sqljdbc_3.0.1301.101_chs.exe, ...

  4. 【Alpha】Scrum Meeting 5-end

    第一天:2019/6/19 前言: 第5次会议在6月19日由PM在教9C-501召开. 总结项目,进行单元测试并进行简单的整合.时长60min. 团队GitHub仓库 仓库连接 1.1 今日完成任务情 ...

  5. 《变革之心》读后感——《Scrum实战》第2次课作业

    刚读了几篇序言.导言和第一个故事,因此读后感可能不全面,先写一下一点儿感受吧. <变革之心>讲的是组织变革,而组织变革是以个人变革为基础的,本书的观点就是在个人变革上,“目睹--感受--变 ...

  6. spring boot配置

    spring.application.name=XXXXXserver.port=9515 # health显示down的问题endpoints.health.sensitive= falsemana ...

  7. Ext.js二级联动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link href ...

  8. 《Nginx高性能Web服务器详解》

    第1章 Nginx初探 第2章 Nginx服务器的安装部署 第3章 Nginx服务器架构初探 第4章 Nginx服务器的高级设置 第5章 Nginx服务器的Gzip压缩 第6章 Nginx服务器的Re ...

  9. gdb调试手册 一 gdb概述

    一 gdb概述 gdb调试器的目的是让你了解其他的程序在执行的时候发生了什么或者其他程序崩溃时正在做什么 gdb主要能够在运行中做四类事情(包括这些事情中的一些附加的事情)来帮助你获取bugs a 运 ...

  10. J2ee项目 编译依赖顺序

    这儿有个帖子, 最后一个回复是:  “我把我项目的libraries的"Order and Export"中的JRE与J2EE顺序换了一个问题解决”. 帖子地址: http://b ...