【LeetCode 438】关关的刷题日记37 Find All Anagrams in a String
关关的刷题日记37 – 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".
题目的意思是要求找出s中所有p的相同字母异序词的起始索引。
思路
思路:刷题日记36——Leetcode 242. Valid Anagram中是判断是否是相同字母异序词。知道了如何判断相同字母异序词之后,这个题目让我们找出s中所有的p的相同字母异序词索引,于是采用滑动窗口的思想:遍历s,判断每一个字符开始的p.size()个字符是否是p的相同字母异序词即可。
class Solution {public:
vector<int> findAnagrams(string s, string p) {
vector<int>m(26,0);
vector<int>temp(26,0);
vector<int>re;
for(char x:p)
{
m[x-'a']++;
}
for(int i=0; i<s.size(); i++)
{
temp=m;
if(temp[s[i]-'a']!=0 && i+p.size()<=s.size())
{
for(int k=i; k<i+p.size();k++)
temp[s[k]-'a']--;
int flag=1;
for(int x:temp)
{
if(x!=0)
{
flag=0;
break;
}
}
if(flag)
re.push_back(i);
else
flag=1;
}
}
return re;
}};
人生易老,唯有陪伴最长情,加油!
以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。
- 正则表达式提取指定内容
- mysql数据库误删除后的数据恢复操作说明
- 在.NET平台上运行Java程序-IKVM.NET入门
- ArraySegment<T>泛型结构示例
- SwitchButton 开关按钮 的多种实现方式
- SVN和Git对比梳理
- linux下expect环境安装以及简单脚本测试
- 泛型List<T>使用示例
- linux系统最小化安装后的初始化脚本
- 简单对比git pull和git pull --rebase的使用
- VS 2010一步步开发windows服务(windows service)
- 运算符重载,以及迭代器[foreach]示例
- 牛津大学王宁博士:大数据与有限理性
- 侃哥:苹果服软了就“降速门”公开致歉并祭出优惠
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 「零门槛多语言 Python/C/C# 通用思想学习系列」第一篇:经典HelloWorld
- 直播系统定制,判断数据连接是否可用
- VS Code 编辑器入门指南上篇-核心概念与组件
- Python turtle库实现基本剖析
- python thinker canvas create_arc 使用详解
- Python3 实现单例设计模式
- Python3 实现建造者模式
- python 实现原型设计模式
- python 最简单的实现适配器设计模式
- python3 最基本且简单的实现组合设计模式
- python 实现装饰器设计模式
- 看得懂的外观设计模式 python3 实现
- 看得懂的设计模式 享元模式python3 最基本(简单)实现
- python3 最简单的实现 模版设计模式
- Qt 第二步 槽与信号(一) 实现点击按钮并弹窗