【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
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". 这个题用到hash和滑动窗口。
以下转载,https://www.cnblogs.com/grandyang/p/6014408.html 原博真的是棒棒哒:
这道题给了我们两个字符串s和p,让我们在s中找字符串p的所有变位次的位置,所谓变位次就是字符种类个数均相同但是顺序可以不同的两个词,
那么我们肯定首先就要统计字符串p中字符出现的次数,
然后从s的开头开始,每次找p字符串长度个字符,来验证字符个数是否相同,如果不相同出现了直接break,
如果一直都相同了,则将起始位置加入结果res中,参见代码如下:
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if (s.empty()) return {};
vector<int> res, cnt(, );
int ns = s.size(), np = p.size(), i = ;
for (char c : p) ++cnt[c];
while (i < ns) {
bool success = true;
vector<int> tmp = cnt;
for (int j = i; j < i + np; ++j) {
if (--tmp[s[j]] < ) {
success = false;
break;
}
}
if (success) {
res.push_back(i);
}
++i;
}
return res;
}
};
我们可以将上述代码写的更加简洁一些,用两个哈希表,分别记录p的字符个数,和s中前p字符串长度的字符个数,然后比较,如果两者相同,则将0加入结果res中,然后开始遍历s中剩余的字符,每次右边加入一个新的字符,然后去掉左边的一个旧的字符,每次再比较两个哈希表是否相同即可,参见代码如下:
解法二:
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if (s.empty()) return {};
vector<int> res, m1(, ), m2(, );
for (int i = ; i < p.size(); ++i) {
++m1[s[i]]; ++m2[p[i]];
}
if (m1 == m2) res.push_back();
for (int i = p.size(); i < s.size(); ++i) {
++m1[s[i]];
--m1[s[i - p.size()]];
if (m1 == m2) res.push_back(i - p.size() + );
}
return res;
}
};
下面这种利用滑动窗口Sliding Window的方法也比较巧妙,首先统计字符串p的字符个数,然后用两个变量left和right表示滑动窗口的左右边界,用变量cnt表示字符串p中需要匹配的字符个数,然后开始循环,如果右边界的字符已经在哈希表中了,说明该字符在p中有出现,则cnt自减1,然后哈希表中该字符个数自减1,右边界自加1,如果此时cnt减为0了,说明p中的字符都匹配上了,那么将此时左边界加入结果res中。如果此时right和left的差为p的长度,说明此时应该去掉最左边的一个字符,我们看如果该字符在哈希表中的个数大于等于0,说明该字符是p中的字符,为啥呢,因为上面我们有让每个字符自减1,如果不是p中的字符,那么在哈希表中个数应该为0,自减1后就为-1,所以这样就知道该字符是否属于p,如果我们去掉了属于p的一个字符,cnt自增1,参见代码如下:
解法三:
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
if (s.empty()) return {};
vector<int> res, m(, );
int left = , right = , cnt = p.size(), n = s.size();
for (char c : p) ++m[c];
while (right < n) {
if (m[s[right++]]-- >= ) --cnt;
if (cnt == ) res.push_back(left);
if (right - left == p.size() && m[s[left++]]++ >= ) ++cnt;
}
return res;
}
};
【easy】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 ...
- 438 Find All Anagrams in a String 找出字符串中所有的变位词
详见:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ C++: class Solution { pu ...
- [LeetCode] 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 ...
- 10.Find All Anagrams in a String(在一个字符串中发现所有的目标串排列)
Level: Easy 题目描述: Given a string s and a non-empty string p, find all the start indices of p's ana ...
- 【leetcode】438. Find All Anagrams in a String
problem 438. Find All Anagrams in a String solution1: class Solution { public: vector<int> fin ...
- 438. Find All Anagrams in a String
原题: 438. Find All Anagrams in a String 解题: 两个步骤 1)就是从s中逐步截取p长度的字符串 2)将截取出的字符串和p进行比较,比较可以用排序,或者字典比较(这 ...
- 438. Find All Anagrams in a String - LeetCode
Question 438. Find All Anagrams in a String Solution 题目大意:给两个字符串,s和p,求p在s中出现的位置,p串中的字符无序,ab=ba 思路:起初 ...
随机推荐
- 软工+C(11): 从命令行开始逐步培养编程能力(Java)
上一篇:助教指南,持续更新... // Version: 0.0.4 许多人,所不知道的是,每一种编程语言都有其对应的单元测试框架,对程序在不同阶段的测试环节也概念模糊.在实际动手编写程序许久之后才听 ...
- 【翻译】asp.net core2.0中的token认证
原文地址:https://developer.okta.com/blog/2018/03/23/token-authentication-aspnetcore-complete-guide token ...
- centos6.5之phpmyadmin安装
PhpMyAdmin 首先我们看一下百度百科,看一下phpmyadmin是做什么的. phpMyAdmin 是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让 ...
- Manifest merger failed : Attribute application@icon value=(@mipmap/ic_launcher) from AndroidManifest
情况是这样子的,导入一个比较老的项目(两年前),它依赖于一个 Libraray,已经先导入了 library,现在导入项目的时候出了错 (1) Android Studio 目前提供将 SDK包成 . ...
- MarkdownPad
MarkdownPad Markdown编辑器,只能在windows下使用 下载地址 :http://markdownpad.com/ 破解: 邮箱:Soar360@live.com 授权证书 GBP ...
- 浅析 Scala 构造器
2019-04-15 关键字:Scala 主构造器.Scala 辅助构造器.Scala 构造器的区别 本篇文章系笔者根据当前所掌握知识对 Scala 构造器的一些心得总结,不保证文章所述内容的绝对.完 ...
- CF集萃2
CF1155D - Beautiful Array 题意:给你一个序列和x,你可以选择任意一个子串(可以为空)乘上x,使得得到的序列最大子串和最大.求这个最大值.30w,2s. 解:设fi,0/1/2 ...
- 快速找出网站中可能存在的XSS漏洞实践
笔者写了一些XSS漏洞的挖掘过程记录下来,方便自己也方便他人. 一.背景 在本篇文章当中会一permeate生态测试系统为例,笔者此前写过一篇文章当中笔者已经讲解如何安装permeate渗透测试系统, ...
- hd RFS USB Sever/ NetworkUSB/16 Pro+ / NET-USB16-P+
s 方法一: 第一步,USB设备lan2和台式机网线对插,设置台式机IP 192.168.1.252.子网掩码255.255.255.0第二部,打开设备USB 固有IP页面192.168.1.250 ...
- Shell中的算数运算
加法 echo $((a+b)) expr $a + $b let "a=1+2";echo $a a=;let "a+=10";echo $a echo &q ...